简体   繁体   English

从Meteor.js打开Websocket连接

[英]Open a Websocket connection from Meteor.js

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 # has no method 'connect' 错误:TypeError:对象#没有方法“连接”


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”

I created a new Meteor package joncursi:socket-io-client to solve this problem. 我创建了一个新的Meteor包joncursi:socket-io-client来解决此问题。 Please see https://atmospherejs.com/joncursi/socket-io-client for more detail and example usage. 请参阅https://atmospherejs.com/joncursi/socket-io-client了解更多详细信息和示例用法。 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. 由于我已经为您捆绑了NPM二进制文件,因此您不必担心安装NPM软件包,声明NPM.require()依赖项等。最重要的是,您可以部署到.meteor.com而无需顺利。

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). 我建议在客户端不允许使用websocket的情况下使用其他类似SockJS或socket.io的东西(因此需要websocket仿真)。

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? ) 根据这个涉及openshift博客帖子的问题的答案,您的答案是:(问题: 如何为客户端设置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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM