是否有不需要使用浏览器的 Node.js 无浏览器 websocket 客户端? [英] Is there a browserless websocket client for Node.js that does not need to use a browser?

查看:23
本文介绍了是否有不需要使用浏览器的 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/d27320502109d0be

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

节点-Websocket-客户端

Peter Griess 的node-websocket-client"做了 75/76 的 hixie 草案,但在撰写本文时,不是 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 客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆