c# websocketServer

颛孙钱青
2023-12-01

代码:

https://github.com/AltairCA/.netcore2.0-websocketServerExample

 

查找安装:WebSocketSharp

解析包:这个可以:

/// <summary>
/// 解析客户端发送来的数据
/// </summary>
/// <returns>The data.</returns>
/// <param name="recBytes">Rec bytes.</param>
/// <param name="length">Length.</param>
private string AnalyzeClientData (byte[] recBytes, int length)
        {
if (length < 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);
        }

这个是不是也可以:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Newtonsoft.Json;
using WebSocketSharp;
using WebSocketSharp.Server;

namespace PrintPlugin
{
    public partial class Form_Main : Form
    {
        public Form_Main()
        {
            InitializeComponent();
        }

        private void Form_Main_Load(object sender, EventArgs e)
        {
            var wssv = new WebSocketServer(54321);
            wssv.AddWebSocketService<PrintServer>("/PrintServer");//添加服务侦听,第一个是类。第二个是调用的接口
            wssv.Start();
            Console.WriteLine("打印服务运行中...");
     
           // wssv.Stop();
        }
        class PrintServer : WebSocketBehavior
        {
            static Dictionary<string, string> dClientAll = new Dictionary<string, string>();
            protected override void OnOpen()
            {
                dClientAll.Add(base.Context.SecWebSocketKey, base.Context.UserEndPoint.ToString());
                Console.WriteLine("连接打开" + dClientAll[base.Context.SecWebSocketKey] + "|" + DateTime.Now.ToString());
                base.OnOpen();
            }
            protected override void OnMessage(MessageEventArgs e)
            {
                var data = e.Data + "|" + DateTime.Now.ToString();
                Console.WriteLine("接收数据" + dClientAll[base.Context.SecWebSocketKey] + "|" + data);
                var retjson = JsonConvert.SerializeObject(new { code = 200, msg = data });
                Send(retjson);

            }
            protected override void OnClose(CloseEventArgs e)
            {
                Console.WriteLine("连接关闭" + dClientAll[base.Context.SecWebSocketKey] + "|" + DateTime.Now.ToString());
                base.OnClose(e);
            }
            protected override void OnError(ErrorEventArgs e)
            {
                Console.WriteLine("错误信息" + dClientAll[base.Context.SecWebSocketKey] + "|" + DateTime.Now.ToString());
                base.OnError(e);
            }
        }
    }
}

 

 类似资料: