[英]Making a connection with Socket.io on Remote Server
I have created a simple app Laravel Homestead to test some basic stuff: Broadcasting events and listening them on socket.io. 我创建了一个简单的应用程序Laravel Homestead,以测试一些基本内容:广播事件并在socket.io上监听它们。
Everything works well. 一切正常。
However, I want to upload the app to a remote server to a directory called learn-redis
and am facing a challenge. 但是,我想将应用程序上载到远程服务器上的一个名为
learn-redis
的目录,并且面临着挑战。 My socket.js file looks like this: 我的socket.js文件如下所示:
var server = require('http').Server();
var io = require('socket.io')(server);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel');
redis.on('message', function(channel, message){
message = JSON.parse(message);
io.emit(channel + ':' message.event, message.data);
});
server.listen(8000);
In Laravel Homestead, I know I can listen to port 8000 because it gets displays on the console when running vagrant up
. 在Laravel Homestead中,我知道我可以监听端口8000,因为运行
vagrant up
时它会在控制台上vagrant up
。 What about my remote server? 那我的远程服务器呢? What port do I use?
我要使用哪个端口?
On client side, I have this code. 在客户端,我有这段代码。
var socket = io(' http://192.168.10.10:8000 '); var socket = io(' http://192.168.10.10:8000 ');
new Vue({ el: '#demo', data: { users: {!! App\\User::all() !!} }, ready: function(){ socket.on('test-channel:App\\\\Events\\\\UserSignedUp', function(data) { this.users.push( data.user ); }.bind(this)); } });
I have pulled Vue.js and Socket.io.js libraries, but when I try doing var socket = io('http://example.com/learn-redis:8000')
I get the error http://example.com:8000/socket.io/?EIO=3&transport=polling&t=LBWnq-U net::ERR_CONNECTION_TIMED_OUT
. 我已经拉了Vue.js和Socket.io.js库,但是当我尝试做
var socket = io('http://example.com/learn-redis:8000')
我得到了错误http://example.com:8000/socket.io/?EIO=3&transport=polling&t=LBWnq-U net::ERR_CONNECTION_TIMED_OUT
。
You need to have a VPS server to be able to install node,most likely, You are on a shared hosting and most providers won't allow you to install node. 您需要具有VPS服务器才能安装节点,最有可能的是,您位于共享主机上,并且大多数提供程序都不允许您安装节点。 Your best bet would be to ask your provider to install node for you or if you have the funds, upgrade to a VPS server.
最好的选择是要求您的提供商为您安装节点,或者如果您有资金,请升级到VPS服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.