readyState表示连接有四种状态:
CONNECTING (0):表示还没建立连接;
OPEN (1): 已经建立连接,可以进行通讯;
CLOSING (2):通过关闭握手,正在关闭连接;
CLOSED (3):连接已经关闭或无法打开;
a.local access rateb.bc(commited burst)c.cir(commited information rate)d.fecn(forward explicit congestion notification)第17题*17、 逆向地址解析协议(inverse arp) 的主要功能是:a.通过网络设备的网络地址查找到设备的硬件地址b.查找连接的dlci编号查找连接的20c 编号c.在帧中继网络中查找虚电路连接的对端设备的协议地址d.在帧中继网络中生成对端协议地址与dlci的映射。组播地址发送协议报文:使用专用的组播地址发送协议报文,因为是在小范围内通讯,所以可以减少对网络中非ospf设备的影响。当客户端请求建立某个c/s应用请求时,rap服务器端会主动push一个java applet到客户端,实时客户端到当前c/s应用的原内网地址信息和端口,协议信息等,客户端java applet把到所有信息通过ssl加密后在internet上传回到rap服务器上,rap服务器解密该数据包,把这些原始信息像在内网的访问一样传给真正的内部c/s服务器,内网c/s应用服务器正常响应此请求,像内网访问一样返回数据包到rap,rap加密此数据包通过internet传输给远程的客户端。

close 方法就是关闭连接;
onopen连接建立,即握手成功触发的事件;
onmessage收到服务器消息时触发的事件;
onerror异常触发的事件;
onclose关闭连接触发的事件;
先说说基于.NET4.5的吧 ashx里面应该是这样写的
- //得到当前WebSocket请求
- HttpContext.Current.AcceptWebSocketRequest(async (context) =>
- {
- WebSocket socket = context.WebSocket;//Socket
- while (true)
- {
- ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
- CancellationToken token;
- WebSocketReceiveResult result =
- await socket.ReceiveAsync(buffer, token);
- if (socket.State == WebSocketState.Open)
- {
- string userMessage = Encoding.UTF8.GetString(buffer.Array, 0,
- result.Count);
- userMessage = "You sent: " + userMessage + " at " +
- DateTime.Now.ToLongTimeString();
- buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(userMessage));
- await socket.SendAsync(buffer, WebSocketMessageType.Text,
- true, CancellationToken.None);
- }
- else { break; }
- }
- });
然后需要注意的是 部署到IIS8上面 转到 Windows程序和功能 -打开Windows功能里面 IIS选项启动4.5 和WebSocket支持 否则会报错误的。
如果你不是Windows8 或者IIS的问题 你可以使用其他方式实现websocket服务器 比如.net socket模拟(因为websocket本身就是基于TCP的完全可以模拟只是规则特别..)
网上有人写了下面一段
- class Program
- {
- static void Main(string[] args)
- {
- int port = 1818;
- byte[] buffer = new byte[1024];
- IPEndPoint localEP = new IPEndPoint(IPAddress.Any, port);
- Socket listener = new Socket(localEP.Address.AddressFamily,SocketType.Stream, ProtocolType.Tcp);
- try{
- listener.Bind(localEP);
- listener.Listen(10);
- Console.WriteLine("等待客户端连接....");
- Socket sc = listener.Accept();//接受一个连接
- Console.WriteLine("接受到了客户端:"+sc.RemoteEndPoint.ToString()+"连接....");
- //握手
- int length = sc.Receive(buffer);//接受客户端握手信息
- sc.Send(PackHandShakeData(GetSecKeyAccetp(buffer,length)));
- Console.WriteLine("已经发送握手协议了....");
- //接受客户端数据
- Console.WriteLine("等待客户端数据....");
- length = sc.Receive(buffer);//接受客户端信息
- string clientMsg=AnalyticData(buffer, length);
- Console.WriteLine("接受到客户端数据:" + clientMsg);
- //发送数据
- string sendMsg = "您好," + clientMsg;
- Console.WriteLine("发送数据:“"+sendMsg+"” 至客户端....");
- sc.Send(PackData(sendMsg));
- Console.WriteLine("演示Over!");
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- /// <summary>
- /// 打包握手信息
- /// </summary>
- /// <param name="secKeyAccept">Sec-WebSocket-Accept</param>
- /// <returns>数据包</returns>
- private static byte[] PackHandShakeData(string secKeyAccept)
- {
- var responseBuilder = new StringBuilder();
- responseBuilder.Append("HTTP/1.1 101 Switching Protocols" + Environment.NewLine);
- responseBuilder.Append("Upgrade: websocket" + Environment.NewLine);
- responseBuilder.Append("Connection: Upgrade" + Environment.NewLine);
- responseBuilder.Append("Sec-WebSocket-Accept: " + secKeyAccept + Environment.NewLine + Environment.NewLine);
- //如果把上一行换成下面两行,才是thewebsocketprotocol-17协议,但居然握手不成功,目前仍没弄明白!
- //responseBuilder.Append("Sec-WebSocket-Accept: " + secKeyAccept + Environment.NewLine);
- //responseBuilder.Append("Sec-WebSocket-Protocol: chat" + Environment.NewLine);
- return Encoding.UTF8.GetBytes(responseBuilder.ToString());
- }
- /// <summary>
- /// 生成Sec-WebSocket-Accept
- /// </summary>
- /// <param name="handShakeText">客户端握手信息</param>
- /// <returns>Sec-WebSocket-Accept</returns>
- private static string GetSecKeyAccetp(byte[] handShakeBytes,int bytesLength)
- {
- string handShakeText = Encoding.UTF8.GetString(handShakeBytes, 0, bytesLength);
- string key = string.Empty;
- Regex r = new Regex(@"Sec\-WebSocket\-Key:(.*?)\r\n");
- Match m = r.Match(handShakeText);
- if (m.Groups.Count != 0)
- {
- key = Regex.Replace(m.Value, @"Sec\-WebSocket\-Key:(.*?)\r\n", "$1").Trim();
- }
- byte[] encryptionString = SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
- return Convert.ToBase64String(encryptionString);
- }
- /// <summary>
- /// 解析客户端数据包
- /// </summary>
- /// <param name="recBytes">服务器接收的数据包</param>
- /// <param name="recByteLength">有效数据长度</param>
- /// <returns></returns>
- private static string AnalyticData(byte[] recBytes, int recByteLength)
- {
- if (recByteLength < 2) { return string.Empty; }
- bool fin = (recBytes[0] & 0x80) == 0x80; // 1bit,1表示最后一帧
- if (!fin){
- return string.Empty;// 超过一帧暂不处理
- }
- bool mask_flag = (recBytes[1] & 0x80) == 0x80; // 是否包含掩码
- if (!mask_flag){
- return string.Empty;// 不包含掩码的暂不处理
- }
- int payload_len = recBytes[1] & 0x7F; // 数据长度
- byte[] masks = new byte[4];
- byte[] payload_data;
- if (payload_len == 126){
- Array.Copy(recBytes, 4, masks, 0, 4);
- payload_len = (UInt16)(recBytes[2] << 8 | recBytes[3]);
- payload_data = new byte[payload_len];
- Array.Copy(recBytes, 8, payload_data, 0, payload_len);
- }else if (payload_len == 127){
- Array.Copy(recBytes, 10, masks, 0, 4);
- byte[] uInt64Bytes = new byte[8];
- for (int i = 0; i < 8; i++){
- uInt64Bytes[i] = recBytes[9 - i];
- }
- UInt64 len = BitConverter.ToUInt64(uInt64Bytes, 0);
- payload_data = new byte[len];
- for (UInt64 i = 0; i < len; i++){
- payload_data[i] = recBytes[i + 14];
- }
- }else{
- Array.Copy(recBytes, 2, masks, 0, 4);
- payload_data = new byte[payload_len];
- Array.Copy(recBytes, 6, payload_data, 0, payload_len);
- }
- for (var i = 0; i < payload_len; i++){
- payload_data[i] = (byte)(payload_data[i] ^ masks[i % 4]);
- }
- return Encoding.UTF8.GetString(payload_data);
- }
- /// <summary>
- /// 打包服务器数据
- /// </summary>
- /// <param name="message">数据</param>
- /// <returns>数据包</returns>
- private static byte[] PackData(string message)
- {
- byte[] contentBytes = null;
- byte[] temp = Encoding.UTF8.GetBytes(message);
- if (temp.Length < 126){
- contentBytes = new byte[temp.Length + 2];
- contentBytes[0] = 0x81;
- contentBytes[1] = (byte)temp.Length;
- Array.Copy(temp, 0, contentBytes, 2, temp.Length);
- }else if (temp.Length < 0xFFFF){
- contentBytes = new byte[temp.Length + 4];
- contentBytes[0] = 0x81;
- contentBytes[1] = 126;
- contentBytes[2] = (byte)(temp.Length & 0xFF);
- contentBytes[3] = (byte)(temp.Length >> 8 & 0xFF);
- Array.Copy(temp, 0, contentBytes, 4, temp.Length);
- }else{
- // 暂不处理超长内容
- }
- return contentBytes;
- }
- }
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-103122-3.html
那我们就真的只剩下麻将了