`
xumingyong
  • 浏览: 182495 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Websocket定义

阅读更多

 

看看W3C在HTML5里对于WebSocket的接口定义:

 

interface WebSocket {
  readonly attribute DOMString URL;

  // ready state
  const unsigned short CONNECTING = 0;
  const unsigned short OPEN = 1;
  const unsigned short CLOSED = 2;
  readonly attribute unsigned short readyState;
  readonly attribute unsigned long bufferedAmount;

  // networking
           attribute Function onopen;
           attribute Function onmessage;
           attribute Function onclose;
  boolean send(in DOMString data);
  void close();
};
WebSocket implements EventTarget;

Web Sockets Now Available In Google Chrome

 

Wednesday, December 09, 2009

Labels: html5, webkit, websockets

 

Starting in the Google Chrome developer channel release 4.0.249.0, Web Sockets are available and enabled by default. Web Sockets are "TCP for the Web," a next-generation bidirectional communication technology for web applications being standardized in part ofWeb Applications 1.0. We've implemented this feature as described in our design docs forWebKit and Chromium.
The Web Sockets API enables web applications to handle bidirectional communications with server-side process in a straightforward way. Developers have been using XMLHttpRequest("XHR") for such purposes, but XHR makes developing web applications that communicate back and forth to the server unnecessarily complex. XHR is basically asynchronous HTTP, and because you need to use a tricky technique like long-hanging GET for sending data from the server to the browser, simple tasks rapidly become complex. As opposed toXMLHttpRequest, Web Sockets provide a real bidirectional communication channel in your browser. Once you get a Web Socket connection, you can send data from browser to server by calling a send() method, and receive data from server to browser by an onmessage event handler. A simple example is included below.
if ("WebSocket" in window) {
  var ws = new WebSocket("ws://example.com/service");
  ws.onopen = function() {
    // Web Socket is connected. You can send data by send() method.
    ws.send("message to send"); ....
  };
  ws.onmessage = function (evt) { var received_msg = evt.data; ... };
  ws.onclose = function() { // websocket is closed. };
} else {
  // the browser doesn't support WebSocket.
}
In addition to the new Web Sockets API, there is also a new protocol (the "web socket protocol") that the browser uses to communicate with servers. The protocol is not raw TCP because it needs to provide the browser's "same-origin" security model. It's also not HTTP because web socket traffic differers from HTTP's request-response model. Web socket communications using the new web socket protocol should use less bandwidth because, unlike a series of XHRs and hanging GETs, no headers are exchanged once the single connection has been established. To use this new API and protocol and take advantage of the simpler programming model and more efficient network traffic, you do need a new server implementation to communicate with — but don't worry. We also developed pywebsocket, which can be used as an Apache extension module, or can even be run as standalone server.
You can use Google Chrome and pywebsocket to start implementing Web Socket-enabled web applications now. We're more than happy to hear your feedback not only on our implementation, but also on API and/or protocol design. The protocol has not been completely locked down and is still in discussion in IETF, so we are especially grateful for any early adopter feedback.
分享到:
评论

相关推荐

    websocket开发测试小工具

    3. **自定义帧格式**:WebSocket定义了自己的帧结构,用于封装和传输数据,包括文本、二进制等多种类型的数据。 4. **握手协议**:WebSocket连接的建立需要通过一个HTTP升级请求完成,这个过程称为握手。握手过程中...

    Websocket测试包.rar

    2. **二进制帧协议**:WebSocket定义了一种二进制帧的数据格式,可以高效地传输文本和二进制数据,减少了协议开销,提高了传输效率。 3. **协议升级**:WebSocket始于一个HTTP Upgrade请求,这意味着可以在现有的...

    WebSocket入门实战工程

    而对于二进制数据,WebSocket定义了Blob和ArrayBuffer两种类型,开发者可以根据需求选择。 在WebSocket的实战工程中,我们需要考虑以下几点: 1. **服务器端实现**:服务器端需要支持WebSocket协议,可以使用Node....

    The WebSocket Protocol RFC

    为了确保连接的正常关闭,WebSocket定义了一个关闭握手的过程。当任何一方希望关闭连接时,可以发送一个特殊的关闭帧,对方接收到这个帧后,也可以选择发送自己的关闭帧作为响应。一旦双方都发送了关闭帧,连接就会...

    WebSocket demo

    4. **自定义操作码**:WebSocket定义了一套自定义的操作码,允许开发者定义自己的扩展和协议。 5. **握手协议**:WebSocket连接的建立需要经过一个TCP三次握手后的“Upgrade”握手过程,确保安全可靠的连接转换。 ...

    webchat(websocket).zip

    4. 自定义帧格式:WebSocket定义了自己的数据帧格式,可以包含多种数据类型,如文本、二进制等。 在Webchat聊天室中,WebSocket用于实现用户之间的即时消息传递。当一个用户发送消息时,消息会被封装成WebSocket帧...

    WebSocket客户端服务器代码

    3. **自定义帧格式**:WebSocket定义了自己的帧结构,用于封装和传输数据。 4. **低延迟**:由于减少了握手次数,WebSocket的数据传输延迟显著降低。 实现WebSocket客户端和服务器的代码通常涉及以下几个步骤: 1....

    WebSocket简单使用.pdf

    - **错误处理和关闭连接(Error Handling and Close Frames)**:WebSocket定义了专门的帧来处理错误和关闭连接,确保了通信的可靠性。 WebSocket在现代Web应用中的应用广泛,例如在线聊天、实时股票报价、在线游戏...

    websocket构建实时web应用

    - **Websocket定义**:Websocket是一种在单个TCP连接上进行全双工通信的协议,它允许客户端和服务端之间的双向数据交换,这使得实现实时交互成为可能。 - **Websocket与HTTP的关系**:虽然Websocket与HTTP都基于TCP...

    WebSocket聊天系统.rar

    3. **自定义帧格式**:WebSocket定义了自己的帧格式,每个数据包都带有标识其类型的帧头,使得协议更加灵活且易于解析。 本聊天系统的组成部分包括“master”(服务端)和“client”(客户端)两部分: 1. **...

    netty-websocket

    4. **自定义操作码**:WebSocket定义了多种操作码,用于区分数据类型,如文本、二进制、关闭连接等。 在Netty-WebSocket项目中,主要包含以下组件和概念: 1. **WebSocketServerHandler**:这是处理WebSocket连接...

    聊天室websocket

    2. **二进制帧协议**:WebSocket定义了自己的二进制帧格式,可以高效地传输文本和二进制数据,不仅支持文本消息,也支持二进制大数据流。 3. **自定义头部**:WebSocket帧头相对简单,可以携带少量元数据,如操作码...

    若依WebSocket集成

    3. **创建WebSocket处理器**:定义一个处理WebSocket消息的类,该类需要继承`AbstractWebSocketMessageBrokerConfigurer`并实现其方法。在这个处理器中,你可以定义接收和发送消息的方法。例如: ```java @Service ...

    spring+websocketdemo

    在WebSocket的支持上,Spring提供了`@MessageMapping`注解,用于定义WebSocket消息处理的方法,以及`WebSocketMessageBrokerConfigurer`接口,用于配置WebSocket消息代理。 1. **Spring WebSocket集成**:在Spring ...

    webSocket_java_websocket同步_websocket_

    创建一个带有`@ServerEndpoint`注解的类,比如`WebSocketServer`,并在其中定义处理逻辑。例如: ```java @ServerEndpoint("/websocket") public class WebSocketServer { @OnOpen public void onOpen...

    MFC websocket server | MFC websocket服务器

    3. **WebSocket协议**:WebSocket协议定义了一种在客户端和服务器之间建立长连接的方法,它在HTTP握手后升级为WebSocket连接,从而实现了双向通信。协议规定了帧格式、错误处理等细节。 4. **帧处理**:在MFC ...

    C# WinForm客户端连接 WebSocket

    首先,需要引入WebSocket4Net库,然后定义一个WebSocket实例,以及连接、发送、接收等方法。 ```csharp using WebSocket4Net; public class WebSocketClient { private WebSocket _webSocket; public void ...

    MFC实现WebSocket通信

    注意,WebSocket协议定义了多种帧类型,如文本帧、二进制帧等,需要正确解析。 5. **事件驱动编程**:为了实现实时通信,你需要设计事件驱动的模型。例如,可以使用MFC的消息映射机制,处理WebSocket连接的打开、...

    Spring+Netty+WebSocket实例

    1. **配置WebSocket**:在Spring的配置文件中,我们需要定义WebSocket的处理器,设置WebSocket路径,并启用WebSocket支持。 2. **创建WebSocket端点**:使用Spring的`@ServerEndpoint`注解定义WebSocket连接的端点...

    WebSocket消息实时提醒

    10. **应用层协议**:有时,开发者还会定义自己的应用层协议,比如消息类型、序列化方式等,以满足特定业务需求。 以上就是关于WebSocket消息实时提醒的相关知识点,涵盖了WebSocket的基本原理、API使用、安全性和...

Global site tag (gtag.js) - Google Analytics