有哪些html5制作的网站,开车小视频素材,网站建设服务合同 律师,网络公司排名前十名有哪些因一些特殊原因研究了下TCP模拟链接websocket。原理上可以连接但具体怎么连接怎么操作就不知道了#xff0c;需要研究下#xff0c;以下是个人研究的方案。
用线上和本地地址来做例子#xff1a;
线上wss地址#xff1a;wss://server.cs.com/cs/vido/1
本地地址ws://127…因一些特殊原因研究了下TCP模拟链接websocket。原理上可以连接但具体怎么连接怎么操作就不知道了需要研究下以下是个人研究的方案。
用线上和本地地址来做例子
线上wss地址wss://server.cs.com/cs/vido/1
本地地址ws://127.0.0.1/cs/vido/1
如果用本地地址来模拟websocket需要遵从websocket协议。WebSocket协议需要通过已建立的TCP连接来传输数据。具体实现上是通过http协议建立通道然后在此基础上用真正的WebSocket协议进行通信所以WebSocket协议和http协议是有一定的交叉关系的。
在tcp与websocket消息交互之前需要先和websocket建立握手关系
GET /cs/vido HTTP/1.1
Host: server.cs.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw
Sec-WebSocket-Version: 13Get输出的是ws后面的子路径地址
Host输出的是域名地址
upgrade、Connection输出的是要升级到websocket通讯
Sec-WebSocket-Key输出的是Base64 encode 的值这个是随机生成的。
Sec-WebSocket-Version输出的是websocket的版本默认必须是13
websocket服务器收到客户端消息后会返回验证消息
HTTP/1.1 101
Connection: upgrade
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Upgrade: websocket
Sec-WebSocket-Accept: sT7MD7zMs7k3yPTFV6JvttwPwoE
客户端接收到服务端发送的新的Sec-WebSocket-Accept参数后使用原来的随机密钥和新的Sec-WebSocket-Accept参数共同生成一个新的Sec-WebSocket-Key参数用于加密数据传输。 客户端将新的Sec-WebSocket-Key参数发送给服务端服务端接收到后使用该参数加密数据传输收到此消息后是代表已经和websocket建立了联系。
这块要注意下如果是用本地地址去测试没问题但用正式地址去测试就会被提示需要ssl加密。
使用ssl加密代码如下 _tcpClient new TcpClient(host, port);_networkStream _tcpClient.GetStream();// 创建SslStreamSslStream sslStream new SslStream(_networkStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);// 验证服务器证书sslStream.AuthenticateAsClient(host);// 验证服务器证书的回调函数public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){if (sslPolicyErrors SslPolicyErrors.None)return true;Console.WriteLine(Certificate error: {0}, sslPolicyErrors);return false;}
用了ssl加密和没用ssl加密的获取的消息也是不一样的如果用了ssl加密后就要用sslStream来接收消息或者发送消息否则会乱码。没有用ssl加密那就用_networkStream来接收或者发送消息。 整体代码如下
public class WebSocketClient2{private TcpClient _tcpClient;private NetworkStream _networkStream;public void ConnectToWebSocket(string host, int port, string resource){_tcpClient new TcpClient(host, port);_networkStream _tcpClient.GetStream();// 创建SslStreamSslStream sslStream new SslStream(_networkStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);// 验证服务器证书sslStream.AuthenticateAsClient(host);// 构建WebSocket握手消息string key Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));string handshakeMessage $GET {resource} HTTP/1.1\r\n $Host:{host}\r\n Upgrade: websocket\r\n Connection: Upgrade\r\n $Sec-WebSocket-Key: {key}\r\n Sec-WebSocket-Version: 13\r\n \r\n;// 发送握手消息byte[] handshakeBuffer Encoding.UTF8.GetBytes(handshakeMessage);//_networkStream.Write(handshakeBuffer, 0, handshakeBuffer.Length);sslStream.Write(handshakeBuffer, 0, handshakeBuffer.Length);// 接收服务器响应并验证byte[] buffer new byte[1024];//int bytesRead _networkStream.Read(buffer, 0, buffer.Length);int bytesRead sslStream.Read(buffer, 0, buffer.Length);string response Encoding.UTF8.GetString(buffer, 0, bytesRead);Console.WriteLine(response);// 验证服务器响应的Sec-WebSocket-Acceptstring expectedResponse Convert.ToBase64String(SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(key 258EAFA5-E914-47DA-95CA-C5AB0DC85B11)));if (response.Contains($Sec-WebSocket-Accept: {expectedResponse})){Console.WriteLine(WebSocket handshake successful.);}else{Console.WriteLine(WebSocket handshake failed.);}byte[] buffer2 new byte[1024];int bytesRead2;while ((bytesRead2 sslStream.Read(buffer2, 0, buffer2.Length)) 0){string message Encoding.UTF8.GetString(buffer2, 0, bytesRead2);// 解析WebSocket消息// ...Console.WriteLine($Received message: {message});}}// 验证服务器证书的回调函数public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){if (sslPolicyErrors SslPolicyErrors.None)return true;Console.WriteLine(Certificate error: {0}, sslPolicyErrors);return false;}}//代码实现调用WebSocketClient2 client new WebSocketClient2();client.ConnectToWebSocket(server.cs.com.cn, 443, /cs/vido/1);//测试环境ws端口号默认是80wss默认端口号是443
文章转载自: http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.tralution.cn.gov.cn.tralution.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.hncrc.cn.gov.cn.hncrc.cn http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.ykrck.cn.gov.cn.ykrck.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.fkffr.cn.gov.cn.fkffr.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.mbzlg.cn.gov.cn.mbzlg.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.bmqls.cn.gov.cn.bmqls.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.rqhn.cn.gov.cn.rqhn.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.rkdzm.cn.gov.cn.rkdzm.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.wkknm.cn.gov.cn.wkknm.cn http://www.morning.mdwlg.cn.gov.cn.mdwlg.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.qxkcx.cn.gov.cn.qxkcx.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.prjns.cn.gov.cn.prjns.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.tqsnd.cn.gov.cn.tqsnd.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.bpmnl.cn.gov.cn.bpmnl.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.tgcw.cn.gov.cn.tgcw.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn