javascript 从 Meteor.js 打开一个 Websocket 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21624149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open a Websocket connection from Meteor.js
提问by Nyxynyx
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'
错误:类型错误:对象 # 没有方法“连接”
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”
回答by Jon Cursi
I created a new Meteor package joncursi:socket-io-client
to solve this problem. Please see https://atmospherejs.com/joncursi/socket-io-clientfor 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
顺利部署到。
回答by Hossein
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?)
根据this question's answer参考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:
Set the DDP_DEFAULT_CONNECTION_URL variable
//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
我为此苦苦挣扎了一段时间,并尝试了不同的方法。在 OpenShift 中对我有用的解决方案是:
设置 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'
回答by alex10
You can try here is the solution: https://github.com/Akryum/meteor-socket-io
你可以试试这里的解决方案:https: //github.com/Akryum/meteor-socket-io
回答by Akshat
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 的情况下(因此需要 websocket 模拟),我建议使用其他类似 SockJS 或 socket.io 的东西。