快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

皇都游戏娱乐:使用Beetle实现HTML5使用的WebSocket服务示例BR服务端代码



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 Testtitle>

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 皇都游戏娱乐= f皇都游戏娱乐unction (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 woldlegend>

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协议.

下载代码

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: