从Meteor.js打开Websocket连接

时间:2022-04-15 18:06:00

How can we open a Websockets connection from Meteor?

我们如何从Meteor打开Websockets连接?

Can we do something like:

我们可以这样做:

ws = new WebSocket('ws://localhost/path');
ws.on('open', function() {
    ws.send('something');
});
ws.on('message', function(message) {
    console.log('received: %s', message);
});

Error: ReferenceError: WebSocket is not defined

错误:ReferenceError:未定义WebSocket


Using socket.io npm package

使用socket.io npm包

var io = Meteor.require('socket.io')
var socket = io.connect('http://localhost');

Error: TypeError: Object #<Object> has no method 'connect'

错误:TypeError:对象#没有方法'connect'


Using ws npm package

使用ws npm包

var WebSocket = Meteor.require('ws');
var ws = new WebSocket('ws://localhost');

Error: Error: Cannot find module '../build/default/bufferutil'

错误:错误:找不到模块'../build/default/bufferutil'

4 个解决方案

#1


4  

I created a new Meteor package joncursi:socket-io-client to solve this problem. Please see https://atmospherejs.com/joncursi/socket-io-client for more detail and example usage. Since I've bundled the NPM binaries into a package for you, you don't have to worry about installing NPM packages, declaring NPM.require() dependencies, etc. And best of all, you can deploy to .meteor.com without a hitch.

我创建了一个新的Meteor包joncursi:socket-io-client来解决这个问题。有关更多详细信息和示例用法,请参阅https://atmospherejs.com/joncursi/socket-io-client。由于我已经将NPM二进制文件捆绑到一个软件包中,因此您不必担心安装NPM软件包,声明NPM.require()依赖项等等。最重要的是,您可以在不使用的情况下部署到.meteor.com一个障碍。

#2


0  

There is a package called Meteor Streams, that can let you do something similar, using the existing meteor websocket to connect to the local server:

有一个名为Meteor Streams的软件包可以让你做类似的事情,使用现有的meteor websocket连接到本地服务器:

chatStream = new Meteor.Stream('chat');

if(Meteor.isClient) {
  sendChat = function(message) {
    chatStream.emit('message', message);
    console.log('me: ' + message);
  };

  chatStream.on('message', function(message) {
    console.log('user: ' + message);
  });
}

I'm not sure you wanted to connect to another server or the local one, if its another one you can use the example you have provided. I would recommend using something else like SockJS or socket.io in the case where websockets aren't permitted on the client side (and hence websocket emulation is required).

我不确定您是否想要连接到另一台服务器或本地服务器,如果是另一台服务器,您可以使用您提供的示例。我建议在客户端不允许使用websockets的情况下使用像SockJS或socket.io这样的东西(因此需要websocket仿真)。

#3


0  

According to this question's answer which refers to an openshift blog post, you answer is: (question : How to set Meteor WebSocket port for clients?)

根据这个问题的答案,这是一个openhift博客文章,你回答的是:(问题:如何为客户设置Meteor WebSocket端口?)

I struggled with this for a while now and I tried different things. The solution that worked for me in OpenShift was this:

我现在挣扎了一段时间,我尝试了不同的东西。在OpenShift中为我工作的解决方案是这样的:

Set the DDP_DEFAULT_CONNECTION_URL variable

设置DDP_DEFAULT_CONNECTION_URL变量

//for http
process.env.DDP_DEFAULT_CONNECTION_URL = 'http://' + process.env.OPENSHIFT_APP_DNS + ':8000'
//for ssl
process.env.DDP_DEFAULT_CONNECTION_URL = 'https://' + process.env.OPENSHIFT_APP_DNS + ':8443'

According to this blog post: https://www.openshift.com/blogs/paas-websockets

根据这篇博文:https://www.openshift.com/blogs/paas-websockets

#4


0  

You can try here is the solution: https://github.com/Akryum/meteor-socket-io

你可以尝试这里的解决方案:https://github.com/Akryum/meteor-socket-io

#1


4  

I created a new Meteor package joncursi:socket-io-client to solve this problem. Please see https://atmospherejs.com/joncursi/socket-io-client for more detail and example usage. Since I've bundled the NPM binaries into a package for you, you don't have to worry about installing NPM packages, declaring NPM.require() dependencies, etc. And best of all, you can deploy to .meteor.com without a hitch.

我创建了一个新的Meteor包joncursi:socket-io-client来解决这个问题。有关更多详细信息和示例用法,请参阅https://atmospherejs.com/joncursi/socket-io-client。由于我已经将NPM二进制文件捆绑到一个软件包中,因此您不必担心安装NPM软件包,声明NPM.require()依赖项等等。最重要的是,您可以在不使用的情况下部署到.meteor.com一个障碍。

#2


0  

There is a package called Meteor Streams, that can let you do something similar, using the existing meteor websocket to connect to the local server:

有一个名为Meteor Streams的软件包可以让你做类似的事情,使用现有的meteor websocket连接到本地服务器:

chatStream = new Meteor.Stream('chat');

if(Meteor.isClient) {
  sendChat = function(message) {
    chatStream.emit('message', message);
    console.log('me: ' + message);
  };

  chatStream.on('message', function(message) {
    console.log('user: ' + message);
  });
}

I'm not sure you wanted to connect to another server or the local one, if its another one you can use the example you have provided. I would recommend using something else like SockJS or socket.io in the case where websockets aren't permitted on the client side (and hence websocket emulation is required).

我不确定您是否想要连接到另一台服务器或本地服务器,如果是另一台服务器,您可以使用您提供的示例。我建议在客户端不允许使用websockets的情况下使用像SockJS或socket.io这样的东西(因此需要websocket仿真)。

#3


0  

According to this question's answer which refers to an openshift blog post, you answer is: (question : How to set Meteor WebSocket port for clients?)

根据这个问题的答案,这是一个openhift博客文章,你回答的是:(问题:如何为客户设置Meteor WebSocket端口?)

I struggled with this for a while now and I tried different things. The solution that worked for me in OpenShift was this:

我现在挣扎了一段时间,我尝试了不同的东西。在OpenShift中为我工作的解决方案是这样的:

Set the DDP_DEFAULT_CONNECTION_URL variable

设置DDP_DEFAULT_CONNECTION_URL变量

//for http
process.env.DDP_DEFAULT_CONNECTION_URL = 'http://' + process.env.OPENSHIFT_APP_DNS + ':8000'
//for ssl
process.env.DDP_DEFAULT_CONNECTION_URL = 'https://' + process.env.OPENSHIFT_APP_DNS + ':8443'

According to this blog post: https://www.openshift.com/blogs/paas-websockets

根据这篇博文:https://www.openshift.com/blogs/paas-websockets

#4


0  

You can try here is the solution: https://github.com/Akryum/meteor-socket-io

你可以尝试这里的解决方案:https://github.com/Akryum/meteor-socket-io