代码:

using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
async static Task Main(string[] args)
{
var client = new ClientWebSocket();
client.ConnectAsync(new Uri("ws://echo.websocket.org"), CancellationToken.None).Wait();
StartReceiving(client);
string line;
while ((line = Console.ReadLine()) != "exit")
{
var array = new ArraySegment<byte>(Encoding.UTF8.GetBytes(line));
await client.SendAsync(array, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
static async void StartReceiving(ClientWebSocket client)
{
while (true)
{
var array = new byte[4096];
var result = await client.ReceiveAsync(new ArraySegment<byte>(array), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Text)
{
string msg = Encoding.UTF8.GetString(array, 0, result.Count);
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("--> {0}", msg);
Console.ForegroundColor = ConsoleColor.DarkGray;
}
}
}
}
}

原文地址

最新文章

  1. 微软移动 Nokia Lumia SensorCore SDK 介绍及上手体验
  2. select标签非空验证,第一个option value=&quot;&quot;即可
  3. .net环境下从PDF文档中抽取Text文本的一些方法汇总
  4. passivedns 安装指南
  5. 改变select组件的option选中状态的快捷方法
  6. Container View Controller
  7. Jquery Ashx 存在缓存问题
  8. 【现代程序设计】【homework-02】【11061027】
  9. OSS.Common扩展.Net Standard支持实例分享
  10. python爬微信公众号前10篇历史文章(1)-思路概览
  11. Open Source BI Platform List
  12. php中获取用户登陆的IP地址以及常规处理
  13. Linux进程管理工具Supervisor
  14. 【算法习题】正整数数组中和为sum的任意个数的组合数
  15. Spring Boot WebSocket从入门到放弃
  16. FreeSWITCH 增删模组
  17. Analysis of FCN
  18. Java LinkedList源码剖析
  19. 洛谷 P4175: bzoj 1146: [CTSC2008]网络管理
  20. dvwa 源码分析(四) --- dvwaPhpIds.inc.php分析

热门文章

  1. java例题_40 字母字符串转数组后排序
  2. 有了CMDB,为什么还需要应用配置管理?
  3. 常见SQL命令总结学习 -- &lt;1&gt;
  4. (5)MySQL进阶篇SQL优化(优化数据库对象)
  5. 茫茫内存,我该如何用 windbg 找到你 ?
  6. 【Prometheus学习笔记】主机监控 -node_exporter
  7. 【Linux】关于Linux的系统编程总结
  8. Day06_27_多态
  9. 数据结构之链表(JavaScript描述)
  10. Apache HTTP Server与Tomcat整合学习记录