WebSockets

  • 版本网址缩略名: WebSockets
  • 版本标题: WebSockets
  • 版本 id: 238851
  • 创建于:
  • 创建者: karsa.si
  • 是否是当前版本?
  • 评论 404 words added

修订内容

WebSockets is a technology that makes it possible to open an interactive communication session between the user's browser and a server.

WebSocket 技术使得浏览器直接与服务器端的程序通过socket通讯成为可能。

Using a WebSocket connection, Web applications can perform real-time communication instead of having to poll for changes back and forth.

使用WebSocket链接,Web应用程序可以实时的推送或者获取信息。

Availability of WebSockets

目前,WebSockets还在完善之中,浏览器支持还未普及,甚至浏览器的不同版本支持情况也不同。(请使用最新的firefox浏览器,翻译这篇文章的时候是13.0)

ExamplesEdit section

<script type="text/javascript">
var websocket;
var connected = false;
function doConnect(wsURI)
{
 if (connected) {
  debug_output_f("<span style='color:red;'>You're already connected!</span>");
 } else {
  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) };
  debug_output_f("CONNECTION REQUESTED ....");
 }
}
function onOpen (evt) {
 connected = true;
 debug_output_f("CONNECTED");
}
function onClose (evt) {
 connected = false;
 websocket.close();
 debug_output_f("DISCONNECTED");
}
function onMessage (evt) {
 debug_output_f("<span style='color: blue;'>RESP: " + evt.data + "</span>");
}
function onError (evt) {
  debug_output_f("<span style='color:red;'>ERROR:</span> " + evt.data);
}
function doSend (msg)
{
 if (connected) {
  websocket.send(msg);
  debug_output_f("SENT: " + msg);
  /*
  { // binary
   size = msg.length;
   var ba = new Uint8Array(size);
   for (var i=0; i<size; i++) {
    ba[i] = msg.charCodeAt(i);
   }
   m = ba.buffer;
   websocket.send(m);
   debug_output_f("SENT: binary message");
  }
  */
 } else {
  debug_output_f("<span style='color: red;'>NOT CONNECTED: No message sent.</span>");
 }
}
function doClose () {
 if (connected) {
  debug_output_f("CLOSING ....");
  websocket.close();
  connected=false;
 } else {
  debug_output_f("<span style='color: red;'>NOT CONNECTED</span>");
 }
}
String.prototype.toByteArray = function() {
 var returnArray = [];
 for (var i=0; i<this.length; i++) {
  returnArray.push(this.charCodeAt(i));
 }
 return returnArray;
}
</script>

修订版来源

<p>WebSockets is a technology that makes it possible to open an interactive communication session between the user's browser and a server.</p>
<p>WebSocket 技术使得浏览器直接与服务器端的程序通过socket通讯成为可能。</p>
<p>Using a WebSocket connection, Web applications can perform real-time communication instead of having to poll for changes back and forth.</p>
<p>使用WebSocket链接,Web应用程序可以实时的推送或者获取信息。</p>
<div id="section_2"> <h2 class="editable"><span>Availability of WebSockets</span></h2> <p>目前,WebSockets还在完善之中,浏览器支持还未普及,甚至浏览器的不同版本支持情况也不同。(请使用最新的firefox浏览器,翻译这篇文章的时候是13.0)</p> <div id="section_5"> <h3 class="editable"><span>Examples</span><a href="/en/WebSockets/Writing_WebSocket_client_applications?action=edit&amp;sectionId=4" title="Edit section"><span class="icon"><img alt="Edit section" class="sectionedit" src="/skins/common/icons/icon-trans.gif"></span></a></h3> <pre class="script-jem">&lt;script type="text/javascript"&gt;
var websocket;
var connected = false;
function doConnect(wsURI)
{
 if (connected) {
  debug_output_f("&lt;span style='color:red;'&gt;You're already connected!&lt;/span&gt;");
 } else {
  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) };
  debug_output_f("CONNECTION REQUESTED ....");
 }
}
function onOpen (evt) {
 connected = true;
 debug_output_f("CONNECTED");
}
function onClose (evt) {
 connected = false;
 websocket.close();
 debug_output_f("DISCONNECTED");
}
function onMessage (evt) {
 debug_output_f("&lt;span style='color: blue;'&gt;RESP: " + evt.data + "&lt;/span&gt;");
}
function onError (evt) {
  debug_output_f("&lt;span style='color:red;'&gt;ERROR:&lt;/span&gt; " + evt.data);
}
function doSend (msg)
{
 if (connected) {
  websocket.send(msg);
  debug_output_f("SENT: " + msg);
  /*
  { // binary
   size = msg.length;
   var ba = new Uint8Array(size);
   for (var i=0; i&lt;size; i++) {
    ba[i] = msg.charCodeAt(i);
   }
   m = ba.buffer;
   websocket.send(m);
   debug_output_f("SENT: binary message");
  }
  */
 } else {
  debug_output_f("&lt;span style='color: red;'&gt;NOT CONNECTED: No message sent.&lt;/span&gt;");
 }
}
function doClose () {
 if (connected) {
  debug_output_f("CLOSING ....");
  websocket.close();
  connected=false;
 } else {
  debug_output_f("&lt;span style='color: red;'&gt;NOT CONNECTED&lt;/span&gt;");
 }
}
String.prototype.toByteArray = function() {
 var returnArray = [];
 for (var i=0; i&lt;this.length; i++) {
  returnArray.push(this.charCodeAt(i));
 }
 return returnArray;
}
&lt;/script&gt;</pre> </div>
</div>
恢复到这个版本