当前有一个需求,需要网页端调用扫描仪,javascript不具备调用能力,因此需要在机器上提供一个ws服务给前端网页调用扫描仪。而扫描仪有一个c#版本的API,因此需要寻找一个c#的websocket库。
java里有大名鼎鼎的netty,通过搜索,c#可以选择websocket-sharp来实现websocket Server。
使用websocket-sharp创建websocket server
using System;using WebSocketSharp;using WebSocketSharp.Server;namespace Example{ public class Laputa : WebSocketBehavior { protected override void OnMessage (MessageEventArgs e) { var msg = e.Data == "BALUS" ? "I've been balused already..." : "I'm not available now."; Send (msg); } } public class Program { public static void Main (string[] args) { var wssv = new WebSocketServer ("ws://dragonsnest.far"); wssv.AddWebSocketService<Laputa> ("/Laputa"); wssv.Start (); Console.ReadKey (true); wssv.Stop (); &nbs