1. WebSockets -- full-duplex communication

The main HTML5 pillars include Markup, CSS3, and JavaScript APIs

For whole set of HTML5, visit html5rocks.com (google product)

 

The URL

The following image shows the WebSocket URL example in tokens:

schema      host              port     server

   ws://example.com:/chat.php

wss is supported as well and is the WebSocket equivalent to https for secure connections (SSL).

Who's using WebSockets

Name            Website                                            Description

Gamooga       http://www.gamooga.com/                  Real-time backend for apps and games

GitLive           http://gitlive.com/                               Notifications on GitHub projects

Superfeedr     http://superfeedr.com/                       Real-time data pushing

Pusher           http://pusher.com/                             Scalable real-time functionality API for web and mobile apps

Smarkets       https://smarkets.com/                        Real-time betting

IRC Cloud      https://www.irccloud.com/                   Chatting

Two great resources containing a large variety of WebSocket demos are as follows:

http://www.websocket.org/demos.html

http://www.html5rocks.com/en/features/connectivity

2. The WebSocket API web client

WebSocket communication and data transmission is bidirectional, so we need two parties to establish it: a server and a client.

HTML5 basics:

  • Markup HTML (.html)

For more information about the HTML5 markup, consider visiting http://html5doctor.com/. There is a complete reference for the supported HTML5 tags at http://html5doctor.com/element-index/.

  • Styling CSS (.css)

http://www.css3.info/ is a great resource for CSS3 and further reading.

  • Logic JavaScript (.js)

The markup defines the structure and the CSS rules apply the styling. What about event handling and user actions? Well, here comes JavaScript! The WebSocket API is pure JavaScript, too!

WebSocket API allows you to connect to a local or remote server, listen for messages, send data, and close the connection. Typical WebSocket workflow shows below:

JavaScript provides an easy way to find out whether a browser can execute WebSocket-specific code:

if (window.WebSocket) {   // validation check

  console.log("WebSockets supported.");                                               

  // Continue with the rest of the WebSockets-specific functionality…

}

else {

  console.log("WebSockets not supported.");

  alert("Consider updating your browser for a richer experience.");

}

Want to see which browsers do support the WebSocket protocol? There is an up-to-date resource available at http://caniuse.com/#feat=websockets.

  • The WebSocket object :var socket = new WebSocket("ws://echo.websocket.org");
  • Events: Open, Message, Close, and Error.

You can handle them either by implementing the onopen, onmessage, onclose, and onerror functions respectively, or by using the addEventListener method.

  • Actions: The WebSocket protocol supports two main actions: send() and close().
  • Properties:

Properties               Description

url                            Returns the URL of the WebSocket  protocol Returns the protocol used by the server

readyState                Reports the state of the connection and can take one of the following selfexplanatory values:

              WebSocket.OPEN

              WebSocket.CLOSED

              WebSocket.CONNECTING

              WebSocket.CLOSING

bufferedAmount     Returns the total number of bytes that were queued when the send() method was called

binaryType             Returns the binary data format we

received                  when the onmessage event was raised

 The complete client example:

index.html:

<!DOCTYPE html>
<html>
<head>
<title>HTML5 WebSockets</title>
<link rel="stylesheet" href="style.css" />
<script src="chat.js"></script>
</head>
<body>
<h1> HTML5 WebSocket chat. </h1>
<input type="text" id="text-view" />
<input type="button" id="send-button" value="Send!" />
<input type="button" id="stop-button" value="Stop" />
</br>
<label id="status-label">Status</label>
</body>
</html>

Chat.js:

 window.onload = function() {
var textView = document.getElementById("text-view");
var buttonSend = document.getElementById("send-button");
var buttonStop = document.getElementById("stop-button");
var label = document.getElementById("status-label");
var socket = new WebSocket("ws://echo.websocket.org");
socket.onopen = function(event) {
label.innerHTML = "Connection open";
}
socket.onmessage = function(event) {
if (typeof event.data === "string") {
label.innerHTML = label.innerHTML + "<br />" + event.data;
}
}
socket.onclose = function(event) {
var code = event.code;
var reason = event.reason;
var wasClean = event.wasClean;
if (wasClean) {
label.innerHTML = "Connection closed normally.";
}
else {
label.innerHTML = "Connection closed with message: " +
reason + " (Code: " + code + ")";
}
}
socket.onerror = function(event) {
label.innerHTML = "Error: " + event;
}
buttonSend.onclick = function() {
if (socket.readyState == WebSocket.OPEN) {
socket.send(textView.value);
}
}
buttonStop.onclick = function() {
if (socket.readyState == WebSocket.OPEN) {
socket.close();
}
}
}

Server side:

Server                                                                  Client
Create a server on localhost:8181  
Start running
Initial handshake: establish connection  <----------- Request connection
Handle the incoming message  --------------------> Send message

refer to book (p. 31):http://www.amazon.com/Getting-Started-HTML5-WebSocket-Programming/dp/1782166963

create dll file in c#

html5 Websockets development guidance的更多相关文章

  1. HTML5分析实战WebSockets基本介绍

    HTML5 WebSockets规范定义了API,同意web使用页面WebSockets与远程主机协议的双向交流. 介绍WebSocket接口,并限定了全双工通信信道,通过套接字网络.HTML5 We ...

  2. HTML5分析实战WebSockets一个简短的引论

    HTML5 WebSockets规范定义了API,同意web页面使用WebSockets与远程主机协议的双向通信. 介绍WebSocket接口,并限定了全双工通信信道,通过套接字网络. HTML5 W ...

  3. 《HTML5编程之旅》系列三:WebSockets 技术解析

    本文主要研究HTML5 WebSockets的使用方法,它是HTML5中最强大的通信功能,定义了一个全双工的通信信道,只需Web上的一个Socket即可进行通信,能减少不必要的网络流量并降低网络延迟. ...

  4. HTML5 postMessage 和 onmessage API 具体应用

    HTML5 postMessage 和 onmessage API 具体应用 随着 HTML5 的发展.了解并熟悉 HTML5 的 API 接口是很重要的.postMessage(send) 和 on ...

  5. 现在就使用HTML5的十大原因

    你难道还没有考虑使用HTML5? 当然我猜想你可能有自己的原因: 它现在还没有被广泛的支持,在IE中不好使,或者你就是喜欢写比较严格的XHTML代码. HTML5是Web开发世界的一次重大的改变,事实 ...

  6. HTML5实现屏幕手势解锁(转载)

    来源:https://github.com/lvming6816077/H5lockhttp://threejs.org/examples/http://www.inf.usi.ch/phd/wett ...

  7. [转]人人网首页拖拽上传详解(HTML5 Drag&Drop、FileReader API、formdata)

    人人网首页拖拽上传详解(HTML5 Drag&Drop.FileReader API.formdata) 2011年12月11日 | 彬Go 上一篇:给力的 Google HTML5 训练营( ...

  8. 使用HTML5的十大原因

    你难道还没有考虑使用HTML5? 当然我猜想你可能有自己的原因:它现在还没有被广泛的支持,在IE中不好使,或者你就是喜欢写比较严格的XHTML代码.HTML5是web开发世界的一次重大的改变,事实上不 ...

  9. Bone.io是一个轻量级的框架构建高性能实时单页HTML5应用程序

    Bone.io允许你使用HTML5 WebSockets构建实时应用程序,提供“热”数据到浏览器.这使您可以轻松地构建丰富的,高度响应的用户界面. 项目主页:http://www.open-open. ...

随机推荐

  1. 【转】 XenServer架构之HA概述

    一.XenServer HA概述 XenServer HA是一套全自动功能设计,规划,安全地恢复出现问题的XenServe 主机上的虚拟机的功能组件. 启用 HA 后,XenServer 将持续监视池 ...

  2. 字符串编辑距离(Levenshtein距离)算法

    基本介绍 Levenshtein距离是一种计算两个字符串间的差异程度的字符串度量(string metric).我们可以认为Levenshtein距离就是从一个字符串修改到另一个字符串时,其中编辑单个 ...

  3. linux下EOF写法梳理

    在平时的运维工作中,我们经常会碰到这样一个场景:执行脚本的时候,需要往一个文件里自动输入N行内容.如果是少数的几行内容,还可以用echo追加方式,但如果是很多行,那么单纯用echo追加的方式就显得愚蠢 ...

  4. React反模式 —— 如何不使用JSX地动态显示组件

    欢迎指导与讨论 : ) 前言 文章的最后能写出以 Modal.open( ) 这种调用形式,动态显示React对话框组件的写法(类似于ant design),同时涉及数据交互(数据能异步地返回给调用者 ...

  5. [LeetCode] Guess Number Higher or Lower II 猜数字大小之二

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  6. sql server 公共表达式的简单应用(cte)

    一.前言 现在做项目数据访问基本都会选择一种orm框架,它以面向对象的形式屏蔽底层的数据访问形式,让开发人员更集中在业务处理上,而不是和数据库的交互上,帮助我们提高开发效率:例如一些简单的insert ...

  7. IT培训行业揭秘(一)

    最近一个多月来,身边有很多朋友问我,我家孩子明年就要大学毕业了,现在工作还没有着落,最近孩子回家经常和我说,他们学校最近来了很多IT培训班,让同学们参加培训,然后各个培训班动辄拿出往届他们的培训学生赚 ...

  8. 文件上传之——用SWF插件实现文件异步上传和头像截取

    之前写过几篇文件上传,那些都不错.今天小编带领大家体会一种新的上传方法,及使用Flash插件实现文件上传. 使用Flash的好处就是可以解决浏览器兼容性问题.之前我写的一个快捷复制功能也是利用的Fla ...

  9. webBrowser1

    HTMLDocument类的引用 using mshtml;

  10. 第五次团队作业——第一次项目冲刺——Alpha版本

    Deadline:2016-11-19    8:00am 本次团队作业将持续三周时间,完成项目Alpha版本,在2016.11.19 的实践课上进行演示操作. 阅读或再次阅读<构建之法> ...