简单实现一个Websocket服务
WebSocket是HTML5里提供的一种便于TCP长连接通讯的协议机制,随着HTML5的普及这种技术的使用会变得越来越广泛,由于Beetle可以灵活地扩展不同的协议,那自然而然对WebSocket支持也是一件非常简单的事情.对于WebSocket的协议分析处理就不详细描述可以参考下载程序有完整协议包代码,如果想更详细了解Websocket的协议可以到这里:http://datatracker.ietf.org/doc/rfc6455/?include_text=1
下面讲述如何使用Beetle非常方便地实现一个基于WebSocket的Hello wold程序.
服务端代码
class Program:WebSocketServer { static void Main(string[] args) { TcpUtils.Setup("beetle"); Program server = new Program(); server.Open(8088); Console.WriteLine("websocket start@8088"); System.Threading.Thread.Sleep(-1); } protected override void OnConnected(object sender, ChannelEventArgs e) { base.OnConnected(sender, e); } protected override void OnDisposed(object sender, ChannelDisposedEventArgs e) { base.OnDisposed(sender, e); } protected override void OnError(object sender, ChannelErrorEventArgs e) { base.OnError(sender, e); } protected override void OnWebSocketReceive(TcpChannel channel, DataPackage e) { Console.WriteLine("MessageType:{0}", e.Type); Console.WriteLine("RequestPath:{0}", e.RequestPath); if (e.Type == PackageType.text) { Console.WriteLine(e.ToString()); DataPackage dp = new DataPackage(); dp.Type = PackageType.text; dp.Data.Encoding("hello " + e.ToString(), Encoding.UTF8); channel.Send(dp); } } }以上就是一个完整的websocket服务,代码非常简单WebSocketServer重写OnWebSocketReceive即可,在事件中根据需要处理对应的DataPackage即可,对于DataPackage的结构和类型实现参考下载代码.
Html代码
<html> <meta charset="utf-8" /> <title>WebSocket Test</title> <script language="javascript" type="text/javascript"> var wsUri = "ws://127.0.0.1:8088/"; var output; function init() { output = document.getElementById("output"); testWebSocket(); } function testWebSocket() { websocket = new WebSocket(wsUri); websocket.onopen = function (evt) { onOpen(evt) }; websocket.onclose = function (evt) { onClose(evt) }; websocket.onmessage = function (evt) { onMessage(evt) }; websocket.onerror = function (evt) { onError(evt) }; } function onOpen(evt) { writeToScreen("CONNECTED"); } function onClose(evt) { writeToScreen("DISCONNECTED"); } function onMessage(evt) { document.getElementById('result').value = evt.data; } function onError(evt) { writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); } function doSend(message) { websocket.send(message); } function writeToScreen(message) { var pre = document.createElement("p"); pre.style.wordWrap = "break-word"; pre.innerHTML = message; output.insertBefore(pre); } function OnConnect() { wsUri = document.getElementById("url").value; init(); } </script> <body> <fieldset> <legend>WebSocket hello wold</legend> <p><span>Server url:</span><input id="url" type="text" value="ws://127.0.0.1:8088/"/><input type="button" value="Connected" onclick="OnConnect()" /></p> <p><span>Enter you name:</span><input id="youname" type="text" value="test"/><input type="button" value="Submit" onclick="doSend(document.getElementById('youname').value)" /></p> <p><span>Result:</span><input id="result" type="text" value=""/></p> </fieldset> <div id="output"> </div> </body> </html>
运行效果
这样一个基于Hello wold的Websocket处理程序就完成.如果对Websocket感兴趣的朋友可以下载源代码了解websocket协议分析的原理,如何从byte[]分析出对应的websocket协议.
访问Beetlex的Github