當(dāng)前有一個需求,需要網(wǎng)頁端調(diào)用掃描儀,javascript不具備調(diào)用能力,因此需要在機(jī)器上提供一個ws服務(wù)給前端網(wǎng)頁調(diào)用掃描儀。而掃描儀有一個c#版本的API,因此需要尋找一個c#的websocket庫。
java里有大名鼎鼎的netty,通過搜索,c#可以選擇websocket-sharp來實現(xiàn)websocket Server。
使用websocket-sharp創(chuàng)建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