| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <%@ WebHandler Language="C#" Class="index" %>
- using System;
- using System.Web;
- using System.Collections.Generic;
- using Fleck;
- using System.Linq;
- /// <summary>
- /// 测试WebSocket服务
- /// xuwei add 2024-03-26
- /// </summary>
- public class index : IHttpHandler
- {
- public static List<IWebSocketConnection> allScokets;
- public static WebSocketServer server;
- public void ProcessRequest(HttpContext context)
- {
- //启动服务
- if (context.Request["start"] is object)
- {
- start();
- context.Response.Write("服务启动成功!");
- }
- //发送数据
- if (context.Request["barcode"] is object)
- {
- foreach (var socket in allScokets.ToList())
- {
- socket.Send("条码:" + context.Request["barcode"].ToString());
- }
- }
- }
- //启动服务
- public static void start()
- {
- allScokets = new List<IWebSocketConnection>();
- server = new WebSocketServer("ws://127.0.0.1:9200");
- server.Start(scoket =>
- {
- scoket.OnOpen = () => { allScokets.Add(scoket); };
- scoket.OnClose = () => { allScokets.Remove(scoket); };
- scoket.OnMessage = message => { allScokets.ToList().ForEach(s => s.Send(message)); };
- });
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
|