本文介绍了是否有不需要使用浏览器的Node.js的无浏览器Websocket客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Socket.IO等都需要在客户端使用浏览器....只是想知道,我们如何为node.js配备无浏览器的websocket客户端?

Socket.IO, etc all require the using of browser on the client side....just wondering, how can we have browserless websocket client for node.js ?

推荐答案

当前推荐

将WebSocket-Node与我的包装器代码一起使用(请参见下文).在撰写本文时,我所知没有其他公共项目支持新的hybi规范,因此,如果要模拟当前的浏览器版本,则需要WebSocket-Node.如果要模拟较旧的浏览器,例如iOS 4.2上的移动Safari,则还需要下面列出的其他库之一,但是您必须自己管理"WebSocket"对象名称冲突.

Use WebSocket-Node with my wrapper code (see below). As of this writing, no other public project that i know of supports the new hybi specification, so if you want to emulate current browser releases, you'll need WebSocket-Node. If you want to emulate older browsers, such as mobile Safari on iOS 4.2, you'll also need one of the other libraries listed below, but you'll have to manage "WebSocket" object name collisions yourself.

以下是node.js的公共WebSocket客户端实现的列表.

A list of public WebSocket client implementations for node.js follows.

Socket.IO

socket.io客户端测试WebSocket实现确实完成了hixie草稿75/76,但在撰写本文时,还没有hybi 7 +.

The socket.io client-test WebSocket implementation does hixie draft 75/76, but as of this writing, not hybi 7+.

https://github.com/LearnBoost/socket.io/blob/master/support/node-websocket-client/lib/websocket.js

我在问他们是否打算更新到hybi 7+: http://groups.google.com/group/socket_io/browse_thread/thread/d27320502109109d0be

i'm asking if they intend to update to hybi 7+:http://groups.google.com/group/socket_io/browse_thread/thread/d27320502109d0be

Node-Websocket-Client

彼得·格里斯(Peter Griess)的"node-websocket-client"确实草拟了75/76草案,但在撰写本文时,还没有hybi 7 +.

Peter Griess's "node-websocket-client" does hixie draft 75/76, but as of this writing, not hybi 7+.

https://github.com/pgriess/node-websocket-client/blob/master/lib/websocket.js

WebSocket节点

Brian McKelvey的WebSocket-Node具有针对hybi 7-17(协议版本7-13)的客户端实现,但是该实现未提供浏览器样式的WebSocket对象.

Brian McKelvey's WebSocket-Node has a client implementation for hybi 7-17 (protocol version 7-13), but the implementation does not provide a browser-style WebSocket object.

https://github.com/Worlize/WebSocket-Node

这是我用来模拟浏览器样式的WebSocket对象的包装器代码:

Here is the wrapper code I use to emulate the browser-style WebSocket object:

/**
 * Wrapper for Worlize WebSocketNode to emulate the browser WebSocket object.
 */
var WebSocketClient = require('./WorlizeWebSocketNode/lib/websocket').client;

exports.WebSocket = function (uri) {
  var self = this;
  this.connection = null;
  this.socket = new WebSocketClient();
  this.socket.on('connect', function (connection) {
    self.connection = connection;

    connection.on('error', function (error) {
      self.onerror();
    });

    connection.on('close', function () {
      self.onclose();
    });

    connection.on('message', function (message) {
      if (message.type === 'utf8') {
        self.onmessage({data:message.utf8Data});
      }
    });

    self.onopen();
  });
  this.socket.connect(uri);
}

exports.WebSocket.prototype.send = function (data) {
  this.connection.sendUTF(data);
}

SockJS

仅供参考,Marek Majkowski的SockJS并不包含节点客户端. SockJS的客户端库只是一个浏览器dom包装器.

Just for reference, Marek Majkowski's SockJS does not include a node client. SockJS's client library is simply a browser dom wrapper.

https://github.com/sockjs/sockjs-client

这篇关于是否有不需要使用浏览器的Node.js的无浏览器Websocket客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 04:41