这一节里我们用一个案例来演示怎么使用 WebSocket 构建一个实时的 Web 应用。这是一个简单的实时多人聊天系统,包括客户端和服务端的实现。客户端通过浏览器向聊天服务器发起请求,服务器端解析客户端发出的握手请求并产生应答 信息返回给客户端,从而在客户端和服务器之间建立连接通道。服务器支持广播功能,每个聊天用户发送的信息会实时的发送给所有的用户,当用户退出聊天室时, 服务器端需要清理相应用户的连接信息,避免资源的泄漏。以下我们分别从服务器端和客户端来演示这个 Web 聊天系统的实现,在实现方式上我们采用了 C# 语言来实现 WebSocket 服务器,而客户端是一个运行在浏览器里的 HTML 文件。
WebSocket 服务器端实现
这 个聊天服务器的实现和基于套接字的网络应用程序非常类似,首先是服务器端要启动一个套接字监听来自客户端的连接请求,关键的区别在于 WebSocket 服务器需要解析客户端的 WebSocket 握手信息,并根据 WebSocket 规范的要求产生相应的应答信息。一旦 WebSocket 连接通道建立以后,客户端和服务器端的交互就和普通的套接字网络应用程序是一样的了。所以在下面的关于 WebSocket 服务器端实现的描述中,我们主要阐述 WebSocket 服务器怎样处理 WebSocket 握手信息,至于 WebSocket 监听端口的建立,套接字信息流的读取和写入,都是一些常用的套接字编程的方式,我们就不多做解释了,您可以自行参阅本文的附件源代码文件。
在描述 WebSocket 规范时提到,一个典型的 WebSocket Upgrade 信息如下所示:
GET /demo HTTP/1.1
Host: example.com
Connection: Upgrade
Sec-WebSocket-Key2: Y3 .P00
Upgrade: WebSocket
Sec-WebSocket-Key1: @ 46546xW%0l
Origin: http://example.com
[-byte security key]
其中 Sec-WebSocket-Key1,Sec-WebSocket-Key2 和 [8-byte security key] 这几个头信息是 WebSocket 服务器用来生成应答信息的来源,依据 draft-hixie-thewebsocketprotocol-76 草案的定义,WebSocket 服务器基于以下的算法来产生正确的应答信息:
- 逐个字符读取 Sec-WebSocket-Key1 头信息中的值,将数值型字符连接到一起放到一个临时字符串里,同时统计所有空格的数量;
- 将在第 1 步里生成的数字字符串转换成一个整型数字,然后除以第 1 步里统计出来的空格数量,将得到的浮点数转换成整数型;
- 将第 2 步里生成的整型值转换为符合网络传输的网络字节数组;
- 对 Sec-WebSocket-Key2 头信息同样进行第 1 到第 3 步的操作,得到另外一个网络字节数组;
- 将 [8-byte security key] 和在第 3,第 4 步里生成的网络字节数组合并成一个 16 字节的数组;
- 对第 5 步生成的字节数组使用 MD5 算法生成一个哈希值,这个哈希值就作为安全密钥返回给客户端,以表明服务器端获取了客户端的请求,同意创建 WebSocket 连接
至 此,客户端和服务器的 WebSocket 握手就完成了,WebSocket 通道也建立起来了。下面首先介绍一下服务器端实现是如何根据用户传递的握手信息来生成网络字节数组的。.NET 平台提供了很方便的对字符串,数值以及数组操作的函数,所以生成字节数组的方法还是非常简单明了的,代码如下:
生成网络字节数组的代码
private byte[] BuildServerPartialKey(string clientKey)
{
string partialServerKey = "";
byte[] currentKey;
int spacesNum = ;
char[] keyChars = clientKey.ToCharArray();
foreach (char currentChar in keyChars)
{
if (char.IsDigit(currentChar)) partialServerKey += currentChar;
if (char.IsWhiteSpace(currentChar)) spacesNum++;
}
try
{
currentKey = BitConverter.GetBytes((int)(Int64.Parse(partialServerKey)
/ spacesNum));
if (BitConverter.IsLittleEndian) Array.Reverse(currentKey);
}
catch
{
if (currentKey!= null) Array.Clear(currentKey, , currentKey.Length);
}
return currentKey;
}
得到网络字节数组以后,服务器端生成 16 位安全密钥的方法如下所示:
生成 16 位安全密钥的代码
private byte[] BuildCompleteServerKey(byte[] serverKey1, byte[] serverKey2,
byte[] last8Bytes)
{
byte[] concatenatedKeys = new byte[];
Array.Copy(serverKey1, , concatenatedKeys, , );
Array.Copy(serverKey2, , concatenatedKeys, , );
Array.Copy(last8Bytes, , concatenatedKeys, , );
System.Security.Cryptography.MD5 MD5Service =
System.Security.Cryptography.MD5.Create();
return MD5Service.ComputeHash(concatenatedKeys);
}
整个实现是非常简单明了的,就是将生成的网络字节数组和客户端提交的头信息里的 [8-byte security key] 合并成一个 16 位字节数组并用 MD5 算法加密,然后将生成的安全密钥作为应答信息返回给客户端,双方的 WebSocekt 连接通道就建立起来了。实现了 WebSocket 握手信息的处理逻辑,一个具有基本功能的 WebSocket 服务器就完成了。整个 WebSocket 服务器由两个核心类构成,一个是 WebSocketServer,另外一个是 SocketConnection,出于篇幅的考虑,我们不介绍每个类的属性和方法了,文章的附件会给出详细的源代码,有兴趣的读者可以参考。
服务器刚启动时的画面如下:
WebSocket 服务器刚启动的画面
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbgAAAAjCAIAAABpfojQAAAHLklEQVR4nO1dS64cNwzsw2Yf5Di+gRc+hI6WRYK2TLKKRUnzwQwLDUNDUcUiqeabF8TJ9evnj3766aeffshz/fr545+//+qnn3766Qc9/w/KC2OMMcYgDjrGBG8hgUQBIZXCX4py8GBKuMOsN64a5Xi+jceBNKv7WMB/g9IPLHGypDD+aBF+5IFCNxKL8JtA3IfH3UFYH6Um3kJOkXDcnxsbb4vqa9gIcH+jNFXzxeVvoD9CHEqDMgz6uEFJttAkeuigPOLsT1WjpMbG26I6KP1VRzf/i5AOyvBjiJ1BaaKYI8o8vZZ+9T5yJ05dnbAsSBJZiFIVhpDzVL6NlyDtO+/vl3Z/HpTzqzjACCNlSitI3nZ/3Myp0jhIyVO7GGttsKbh5pqnysXWeJ3zQZRCyHnwVfFURMlCkZH+h/KX/M/mS8jnvod2IlLfrWrTyav+h/Em3ygVqs1LjDgVhvBu6elUBZtMuXJRpKfiiXiSU2kaAciirKv8inGTv+R/Nt80+hHPU2J4NTb9z2N5UA4ZdyyFJBSZ3psFMeJB7x/KI7s6EA9XEp4ilbzctSP6Q05SHxGIwahK1zv8Stwq/46enXxJXLMeGOEpzrmsygTl9az6PwQv/9Wbd8g4E6rwVFrKaqFRuDU2PQqxoPqkzJxB8d+E7w65G+TUGr+3i3dmQQnX4+Ou6UFBzcJviXZlV9GD8gozrfo/Ci//1Xs+Zda+LmlEXusFzaEzEva8toGfOl6SP+XX4rt0PE3SLJMaP6LzE3IStKS/6n9QDwp6/dk7BHOEEy6L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pcnNN+8+iLZ41cS1ooHoMZiPoLOEx9OGFtEYRkQL0X8Z1ZpXCbmdGG+I/KIYQk6C6vwL/kf08H5xi+KPtgjPLF5pZbWYz4YyKMOelWqNnNOWoOpzPZ4qXMx/kugo4vXnbUBAB0N5SBLyJ3EVmIhia1K2TTvaqkpC/SJrpaGP0GPiVvWMCGlc3T/Vz539Wg/6LjD/K4hLfltIX4nDAv+CHr8VLuY/EfzNQKHn3g8HQhuq0v2JnnDXe86hfb6heNGT2KtbYkY6ibKu8j9HD+9vtXSlpitGv3WvSV4L/X0q9EFJEiudfQQ/8rm30gUCokLGW0N6ZBaciin5z84pSr1D/Cgcl6Gfquqp8pzif60eJcqav9Ip4xNuKf5c7cvAB+U1dYVklfpX7cbi12E1Eb9f3+KNgydMLWGIMKjRqXCi4qDFAkwrjT2sM1Jbshv4LdE/PKLwkxBI7ZvoITCnuE7dn2jgSc0f/RY/9V74+L+Zc7fkmvqNOucPVo0mkF9XA4U3KYQnaTQaZ/DZg5IMLOR20XGGhpoZWNUR1oOv0XhrfPagrIJPqB5ejcaXogdlo9FocPwLJ9/fF4F5Qr4AAAAASUVORK5CYII=" alt="" />
客户端可以依据这个信息填写聊天服务器的连接地址,当有客户端连接到聊天服务器上时,服务器会打印出客户端和服务器的握手信息,每个客户的聊天信息也会显示在服务器的界面上,运行中的聊天服务器的界面如下:
有客户端连接到 WebSocket 服务器的
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnMAAAEDCAIAAADRGBUFAAAgAElEQVR4nO2dTZbrOI9Etdie9+nl9A560IvQ0r7Bq/JTkkAwQFGynL53UEemwECAkolyPv9s//9///s///1fe86WI2aF018P+4PwoU4UholcQr9JpGN03jOE6+OsST8iZol0Ol4PwmOpPg0B4Cx/OuvWPc36Z6PesvspIqDUWcOk13VWcSprXZd21iXB/axqluEgPJZqZ+1v9ezOB4CYYWcNH4ac6axNlmaK04C35EWb3hSWbCKr9ppwWTJL4sC06iiEmqvqhbcwvO76+nL1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+GjxtNYPOCgC/Df1NEf14Rqk/neysYrzUWUUv0Xl7nYs663Zoh2EWX+coFfoJj7ORhdBZAeC3Mfw8q7/xlSLDeL+jhG63dZ1V6IQ2/IXaO0yR5lSo0wsO/WzJRRcBqyitGwDAx2B+I7/e+0obuthMwybXZxeWwkYVZjx2I62Tl3I5c52ymkKMOFcfAAB+8Oqs/R7abOXhzi56ZDg926PD7MK21gkDhOCwDb+RSy05ms9ZCgCAz6D51M3rQL+SEzFhcDbinwUAAPgM+OVzAACAldBZAQAAVkJnBQAAWAmdFQAAYCH/AdvtY+ujvQ9fAAAAAElFTkSuQmCC" alt="" />
以上我们简单描述了实现一个 WebSocket 服务器的最基本的要素,下一节我们会描述客户端的实现。
客户端实现
客 户端的实现相对于服务器端的实现来说要简单得多了,我们只需要发挥想象去设计 HTML 用户界面,然后呼叫 WebSocket JavaScript 接口来和 WebSocket 服务器端来交互就可以了。当然别忘了使用一个支持 HTML5 和 WebSocket 的浏览器,在笔者写这篇文章的时候使用的浏览器是 Firefox。客户端的页面结构是非常简洁的,初始运行界面如下:
聊天室客户端初始页面
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtsAAAIJCAIAAAD75GbbAAAgAElEQVR4nO3d344k130f8HoKvgGhl+ADBAF4afAVbMAwkAFvRRMBdSELFmLEBiy0xAtHtmMglExEXtNjG47tKApiWpbtloPEtmjRJLWcWe7OzO4sqeUqnYvZna2u86dOVXfPr2f280Fh0Vt16tSp6pr6fbe6erZbAQBE66IHAAAgkQAAe0AiAQDiSSQAQDyJBACIJ5EAAPEkEgAgnkQCAMSTSACAeBIJABBPIgEA4kkkAEA8iQQAiCeRAADxJBIAIJ5EAgDEk0gAgHgSCQAQTyIBAOJJJABAPIkEAIgnkQAA8SQSaLBcLpfLZfQoAG4wiYRAR0dHR0dH0aNoslx0FxZyCcAuXK9EclkVnqvacAP2Ol/Ojw4Puq7ruoPDKwglT4cw//A9He4VDRjgOZNPJM+uvZUyOCiU2et00tFm1/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kF/ngLDulCSMJHPm1Sh32p/Pz09OTo+PbP5ZI9oREAsDOlRPJ2fn52fn5aX3quq6yaLC00rg/PXhwcnLv46Of/ItEsickEgB2rpRILsNBfUrvjgyWZtukLfvT/fv37t29/fFHEsm+kEgA2LlSInnw4N6DB/fu379bn7quu/yz/6L018Gc7HR29sndTz66/eGPJJI9IZEAsHOlRHIZDipT13X9F5d/TRukjSvT6emdT+58+JMP/lki2RMSCQA7V0okZ2d3z87unp5+Upm6rsu+TmcOlmYbX04nJ3fuHH/40b9KJPtCIgFg58qJ5MntitLUdd3li4vXly/6DQYzW6aTk+M7xx989K//JJHsCYkEgJ0rJZLLcJCduq67+PPixWDR5dLBzIFS5/fufXx89K8fvv+PEsmekEgA2LlSIjk5OT45OQqZ7t29fXT7xx+8/38kkj0hkQCwc6VEcuf4o+PjD4+PP7xz/OGd44+ubDo+/vDj2z/+4P3/+y//vJRI9oREAsDOpWXlwdm9F15+7c++97/ef+8f/uVHyx//6Ifvv/cPVzb9+Ec/fO+f/u4f//df3/rTP33h5ddCjgkDEgkAO5eWlU/P7//8l7/5b//df/xP3/rOP/zgu1c8/fAH//2Hf/OXb//hrX/zS//hF3/1d0KOCQPFRGIymUwm0xanQaH5/PNHP/3s/utf//0XX3mje+nVkOmFl1/7pa/+7qNH57uvtozLJxIAgKskkQAA8SQSACCeRAIAxJNIAIB4EgkAEE8iAQDiSSQAQDyJBACIJ5EAAPEkEgAgnkQCAMSTSACAeBIJABBPIgEA4kkkAEA8iQQAiCeRAADxJBIAIJ5EAgDEk0gAgHgSCQAQTyIBAOJJJABAPIkEAIgnkQAA8SQSACCeRAIAxJNIAIB4/x9uMt4jHzgxyQAAAABJRU5ErkJggg==" alt="" width="476" height="339" />
当页面初次加载的时候,首先会检测当前的浏览器是否支持 WebSocket 并给出相应的提示信息。用户按下连接按钮时,页面会初始化一个到聊天服务器的 WebSocekt 连接,初始化成功以后,页面会加载对应的 WebSocket 事件处理函数,客户端 JavaScript 代码如下所示:
初始化客户端 WebSocket 对象的代码
function ToggleConnectionClicked() {
if (SocketCreated && (ws.readyState == 0 || ws.readyState == 1)) {
ws.close();
} else {
Log("准备连接到聊天服务器 ...");
try {
ws =
new WebSocket("ws://" + document.getElementById("Connection").value);
SocketCreated = true;
} catch (ex) {
Log(ex, "ERROR");
return;
}
document.getElementById("ToggleConnection").innerHTML = "断开";
ws.onopen = WSonOpen;
ws.onmessage = WSonMessage;
ws.onclose = WSonClose;
ws.onerror = WSonError;
}
}; function WSonOpen() {
Log("连接已经建立。", "OK");
$("#SendDataContainer").show("slow");
}; function WSonMessage(event) {
Log(event.data);
}; function WSonClose() {
Log("连接关闭。", "ERROR");
document.getElementById("ToggleConnection").innerHTML = "连接";
$("#SendDataContainer").hide("slow");
}; function WSonError() {
Log("WebSocket错误。", "ERROR");
};
当用户按下发送按钮,客户端会调用WebSocket对象向服务器发送信息,并且这个消息会广播给所有的用户,实现代码如下所示:
function SendDataClicked()
{
if (document.getElementById("DataToSend").value != "") {
ws.send(document.getElementById("txtName").value + "说 :\"" +
document.getElementById("DataToSend").value + "\"");
document.getElementById("DataToSend").value = "";
}
};
如果有多个用户登录到聊天服务器,客户端页面的运行效果如下所示:
聊天客户端运行页面
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtQAAAH7CAIAAABjXZ/ZAAAgAElEQVR4nO3d344k12Ef4HoKvgGhl+ADBAF4afAVbMAwkAZvRRMBdSELFmLEBiy0xAtHtmMglExEXtNtG47tKApiWpbtloPEtmjRJLXsWe7OzO4sqeUqnYvZnamu87equ0/3zn4fCoOaqlOnTv3pOr+pru7p1gAADXWHbgAA8HwRPgCApoQPAKAp4QMAaEr4AACaEj4AgKaEDwCgKeEDAGhK+AAAmhI+AICmhA8AoCnhAwBoSvgAAJoSPgCApoQPAKAp4QNKlsvlcrk8dCsAbgzhg0NZrVar1erQraiynHeX5iIIwPaeofBx1QE8V93ADdjqeM+9Wsy6ruu62aJB/njahOm772lzGzUY4EaLhI/ry2ymx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NCR8AQFPCBwDQlPABADQlfAAATQkfAEBTwgcA0JTwAQA0JXwAAE0JHwBAU8IHANCU8AEANCV8AABNCR8AQFP/HxGq6Ap1fKWVAAAAAElFTkSuQmCC" alt="" width="530" height="371" />
至此我们已经完成了一个完整的 WebSocket 客户端实现,用户可以体验一下这个聊天室的简单和快捷,完全不用考虑页面的刷新和繁琐的 Ajax 调用,享受桌面程序的用户体验。WebSocket 的强大和易用可见一斑,您完全可以在这个基础上加入更多的功能,设计更加漂亮的用户界面,切身体验 WebSocket 的震撼力。完整的客户端代码请参阅附件提供的源代码。
WebSocket 的局限性
WebSocket 的优点已经列举得很多了,但是作为一个正在演变中的 Web 规范,我们也要看到目前用 Websocket 构建应用程序的一些风险。首先,WebSocket 规范目前还处于草案阶段,也就是它的规范和 API 还是有变动的可能,另外的一个风险就是微软的 IE 作为占市场份额最大的浏览器,和其他的主流浏览器相比,对 HTML5 的支持是比较差的,这是我们在构建企业级的 Web 应用的时候必须要考虑的一个问题。
总结
本文介绍了 HTML5 WebSocket 的横空出世以及它尝试解决的的问题,然后介绍了 WebSocket 规范和 WebSocket 接口,以及和传统的实时技术相比在性能上的优势,并且演示了怎样使用 WebSocket 构建一个实时的 Web 应用,最后我们介绍了当前的主流浏览器对 HTML5 的支持情况和 WebSocket 的局限性。不过,我们应该看到,尽管 HTML5 WebSocket 目前还有一些局限性,但是已经是大势所趋,微软也明确表达了未来对 HTML5 的支持,而且这些支持我们可以在 Windows 8 和 IE10 里看到,我们也在各种移动设备,平板电脑上看到了 HTML5 和 WebSocket 的身影。WebSocket 将会成为未来开发实时 Web 应用的生力军应该是毫无悬念的了,作为 Web 开发人员,关注 HTML5,关注 WebSocket 也应该提上日程了,否则我们在新一轮的软件革新的浪潮中只能做壁上观了。
WebSocket简单介绍(WebSocket 实战)(3)的更多相关文章
-
WebSocket简单介绍(WebSocket JavaScript 接口)(2)
上一节介绍了 WebSocket 规范,其中主要介绍了 WebSocket 的握手协议.握手协议通常是我们在构建 WebSocket 服务器端的实现和提供浏览器的WebSocket 支持时需要考虑的问 ...
-
HTTP协议系列(3)---包括WebSocket简单介绍
一.HTTPS HTTP是超文本传输协议,那HTTPS是什么尼?要明白HTTPS是什么先要明白HTTP的缺点,想一下我们在使用HTTP的时候会有那些缺点尼? 1.通信使用的明文(不加密),内容 ...
-
WebSocket简单介绍
Java后端WebSocket的Tomcat实现 一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSoc ...
-
WebSocket简单介绍(1)
HTML5作为下一代WEB标准,拥有许多引人注目的新特性,如Canvas.本地存储.多媒体编程接口.WebSocket等等.今天我们就来看看具有“Web TCP”之称的WebSocket. WebSo ...
-
webSocket 简单介绍
WebSocket :WebSocket协议支持(在受控环境中运行不受信任的代码的)客户端与(选择加入该代码的通信的)远程主机之间进行全双工通信. 简单的说 ...
-
HTML5 WebSocket 技术介绍
WebSocket是html5规范新引入的功能,用于解决浏览器与后台服务器双向通讯的问题,使用WebSocket技术,后台可以随时向前端推送消息,以保证前后台状态统一,在传统的无状态HTTP协议中,这 ...
-
SpringBoot2+WebSocket之聊天应用实战(优化版本)
背景 之前再SpringBoot2.0集成WebSocket,实现后台向前端推送信息中已经进行过一次demo,而这次的demo更加明确,优化了相关代码,为IM而生 前提 前提当然是导入相关的包,以及配 ...
-
WebSocket简单使用
很简单就不写太多介绍了,仅展示服务端代码!! Configure在Startup类的方法中添加WebSockets中间件 app.UseWebSockets(); 可以配置以下设置: KeepAliv ...
-
服务端向客户端推送消息技术之websocket的介绍
websocket的介绍 在讲解WebSocket前,我们先来看看下面这种场景,在HTTP协议下,怎么实现. 需求: 在网站中,要实现简单的聊天,这种情况怎么实现呢?如下图: 当发送私信的时候,如果要 ...
随机推荐
-
gulp初学
原文地址:gulp初学 至于gulp与grunt的区别,用过的人都略知一二,总的来说就是2点: 1.gulp的gulpfile.js 配置简单而且更容易阅读和维护.之所以如此,是因为它们的工作方式不 ...
-
Selenium学习笔记之外部化相关测试数据---xml
我们也可以用xml来定义一个信息更为丰富的UIMap.xml文件,比如,额外还添加控件所属的页,控件的类型,然后解析构建一个XMLParser类来读取相应的值. <?xml version=&q ...
-
[转载]--Ubuntu下修改DNS重启也能用的方法
安装好Ubuntu之后设置了静态IP地址,再重启后就无法解析域名.想重新设置一下DNS,打开/etc/resolv.conf cat /etc/resolv.conf# Dynamic resolv. ...
-
Java-Android 之Hello World
1.新建一个Android Project 2.2版本的 修改values下面的内容,为: <?xml version="1.0" encoding="utf-8& ...
-
Mapreduce运行过程分析(基于Hadoop2.4)——(一)
1 概述 该瞅瞅MapReduce的内部执行原理了,曾经仅仅知道个皮毛,再不搞搞,不然怎么死的都不晓得.下文会以2.4版本号中的WordCount这个经典样例作为分析的切入点.一步步来看里面究竟是个什 ...
-
利用webBrowser获取框架内Html页面内容
原文:利用webBrowser获取框架内Html页面内容 利用webBrowser获取页面比较简单,MSDN下有示例,在这里不必多说. 可是一些 HTML 文档由“框架”构成,或可以存放它们自己独特 ...
-
ajax面试题
1.ajax是什么?如何创建一个ajax? ajax并不算是一种新的技术,全称是asynchronous javasript and xml,可以说是已有技术的组合,主要用来实现客户端服务器的异步通信 ...
-
UWP 拖拽文件
桌面环境下的UWP,加入拖拽模式还是会增加用户好感度的. 好了,先看一下我最新研发的[小微识别]吧,演示一下 炫酷,有没有,
-
content-type: application/json没有设置导致的500错误
$.ajax({ url:'http://xxx.test', type: 'Post', data: JSON.stringify(model), dataType: 'json', content ...
-
Makefile依赖关系中的竖线“|”
网上搜索无果,于是自己查看了一下makefile的info文件,其中解释如下: [java] view plain copy print? target : prerequisites [TAB] ...