项目问题接触了写webSocket的一些东西,先记录下来(功能是pc端添加一条数据然后微应用端(h5)接受数据,需要在pc端打开websocket服务器把参数传过去,然后微应用端链接webSocket服务器后服务器再把参数传给微应用端)
这里是pc端添加数据后执行的打开webSocket服务器的代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using HF.Cloud.Model; using System.Data; using System.Web.UI.WebControls; using System.IO; using Newtonsoft.Json; using System.Web.Script.Serialization; using System.Data.SqlClient; using System.Diagnostics; using System.Threading; using HF.Cloud.CommonDAL; using System.Transactions; //using 挺多需要哪些用哪些吧 Thread ta = new Thread(new ThreadStart(delegate () { try { HF.Cloud.BLL.Common.Logger.Error("webSocket调试start!"); //string rootPath = @"D:\dingding\bin\HF.Clound.DingDingForm.exe"; string rootPath = @"D:\易维客\AssetCloudSolutionV1.5\HF.Clound.DingDingForm\bin\Release\HF.Clound.DingDingForm.exe"; string fileName = rootPath; Process pro = new Process(); //pro.StartInfo.CreateNoWindow = true; pro.StartInfo.UseShellExecute = true;//true显示应用窗口,false不显示窗口 pro.StartInfo.FileName = fileName; pro.StartInfo.Arguments = "121 dfs";//用空格来分割参数 HF.Cloud.BLL.Common.Logger.Error("webSocket调试:" + pro.StartInfo.Arguments); pro.Start(); pro.WaitForExit();//一直打开不会退出 HF.Cloud.BLL.Common.Logger.Error("webSocket调试结束"); } catch (Exception ex) { throw ex; } })); ta.Start();
这里是webSecket服务器端的代码,通过pro.Start();可以打开此exe服务端应用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.Text.RegularExpressions; using System.Security.Cryptography; using System.Diagnostics; namespace HF.Clound.DingDingForm { class Program { static string byte_to_string(byte[] b) { string s = ""; foreach (byte _b in b) { s += _b.ToString(); } return s; } //其他项目打开的此exe应用程序,参数都在args里面 static void Main(string[] args) { //传过来的值都在args里面 HF.Cloud.BLL.Common.Logger.Error("ddForm进来了!"); Console.WriteLine("ddForm进来了!"); Console.WriteLine("参数个数:" + args.Length); HF.Cloud.BLL.Common.Logger.Error("ddForm进来了!" + args); Console.WriteLine("ddForm进来了!" + args); // HF.Cloud.BLL.Common.Logger.Error("第一个值:" + args[0]); //Console.WriteLine("第一个值:"+args[0]); //Console.WriteLine("第二个值:" + args[1]); if (args.Length > 0)//传过来的值都在args里面,可以直接用的 { for (int i = 0; i < args.Length; i++) { Console.WriteLine("args-" + i + ":" + args[i]); } } //Console.Read(); int port = 8124;//这里是端口,端口是自己设置的,只要跟自己本机不冲突的端口号都可以用 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("已经发送握手协议了...."); string result01 = ""; if (args.Length > 0) { for (int i = 0; i < args.Length; i++) { //Console.WriteLine("args-" + i + ":" + args[i]); result01 += args[i]; result01 += "-"; } } //发送数据 //string sendMsg = "您好," + clientMsg; //Console.WriteLine("发送数据:“" + sendMsg + "” 至客户端...."); //sc.Send(PackData(sendMsg)); string sendMsg = result01; sc.Send(PackData(sendMsg)); //接受客户端数据 //Console.WriteLine("等待客户端数据...."); length = sc.Receive(buffer);//接受客户端信息 string clientMsg = AnalyticData(buffer, length); if (clientMsg == "success") { //this.Close(); //System.Environment.Exit(0); Process.GetCurrentProcess().Kill();//此方法完全奏效,绝对是完全退出。 } Console.WriteLine("接受到客户端数据:" + clientMsg); Console.WriteLine("演示Over!"); Console.Read(); } catch (Exception e) { // HF.Cloud.BLL.Common.Logger.Error(e.ToString()); 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; } } }
这里是微应用(h5)前端代码
<script type="text/javascript"> var ws; if(typeof(WebSocket) == "undefined") { alert("您的浏览器不支持WebSocket"); } function ToggleConnectionClicked() { try { //var SOCKECT_ADDR = "ws://localhost:9192/chat"; //直接写端口后,后面的就不需要了 //var SOCKECT_ADDR = "ws://192.168.1.41:8124";// 如果是发布的网站,就需要用这个Ip var SOCKECT_ADDR = "ws://localhost:8124";//在本机就用localhost,端口要用应用程序里设置的端口 ws = new WebSocket(SOCKECT_ADDR); ws.onopen = function (event) { alert("已经与服务器建立了连接\r\n当前连接状态:" + this.readyState); }; ws.onmessage = function (event) { alert("接收到服务器发送的数据:\r\n" + event.data); }; ws.onclose = function (event) { alert("已经与服务器断开连接\r\n当前连接状态:" + this.readyState); }; ws.onerror = function (event) {alert("WebSocket异常!" + event.data);}; } catch (ex) { alert(ex.message); } }; function SendData() { try { ws.send("success"); } catch (ex) { alert(ex.message); } }; function seestate() { alert(ws.readyState); //判断websocket状态,1是链接状态 } </script> <button id='ToggleConnection' type="button" onclick='ToggleConnectionClicked();'> 连接服务器</button><br /> <br /> <button id='ToggleConnection01' type="button" onclick='SendData();'> 发送我的名字:beston</button><br /> <br /> <button id='ToggleConnection02' type="button" onclick='seestate();'> 查看状态</button><br /> <br />
转载于:https://blog.51cto.com/taotao1101/1942997