解决方案:5分
解决方案:20分
参考开源项目: websocket-sharp
using System; using WebSocketSharp; namespace Example { public class Program { public static void Main (string[] args) { using (var ws = new WebSocket ("ws://dragonsnest.far/Laputa")) { ws.OnMessage += (sender, e) => Console.WriteLine ("Laputa says: " + e.Data); ws.Connect (); ws.Send ("BALUS"); Console.ReadKey (true); } } } }
解决方案:15分
上面的是客户端代码,服务器代码示例如下
using System; using WebSocketSharp; using WebSocketSharp.Server; namespace Example { public class Laputa : WebSocketService { 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 (); } } }