Laravel 5 - Homestead - Redis - NodeJs - Socket IO玩得不好

时间:2022-06-07 19:24:25

I am attempting to get some real time notifications into my Laravel 5 app. Building locally on a vagrant box with Homestead.

我试图在我的Laravel 5应用程序中获得一些实时通知。在Homestead的一个流浪盒上进行本地建设。

I can not work out what the issue is with the following set up.

我无法解决以下设置的问题。

My server.js is as follows ...

我的server.js如下......

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');

server.listen(3000);

io.on('connection', function (socket) {

  var redisClient = redis.createClient();
  redisClient.subscribe('message');

  redisClient.on("message", function(channel, message) {
    socket.emit(channel, message);
  });

  socket.on('disconnect', function() {
    redisClient.quit();
  });

});

Then my client code is to subscribe:

然后我的客户端代码是订阅:

<script src="https://cdn.socket.io/socket.io-1.3.4.js"></script>

<script>
var socket = io.connect('http://127.0.0.1:3000');
socket.on('message', function (data) {
   alert(data);
});
</script>

Firebug keeps returning:

Firebug不断返回:

GET http://127.0.0.1/socket.io/?EIO=3&transport=polling&t=1430825965165-1 Aborted

获取http://127.0.0.1/socket.io/?EIO=3&transport=polling&t=1430825965165-1已中止

Chrome tools shows me the following:

Chrome工具向我展示了以下内容:

http://127.0.0.1:3000/socket.io/?EIO=3&transport=polling&t=1430826106108-3 net::ERR_CONNECTION_REFUSED

Pretty new to NodeJs and very confused. Spent a good 4 hours now trying to work it out.

对NodeJs来说很新,很困惑。花了4个小时试着解决它。

Any pointers?

many thanks, Dean.

非常感谢,Dean。

2 个解决方案

#1


First make sure that you have installed all dependencies of your server.js file, if you are working with Homestead, execute the following command in your root project directory:

首先确保已安装server.js文件的所有依赖项,如果您正在使用Homestead,请在根项目目录中执行以下命令:

npm install express redis socket.io --save

npm install express redis socket.io --save

Wait until installation finish and start the server with:

等到安装完成并启动服务器:

node server.js

#2


Use your project's virtual host name for connect with socket. It works for me. Edit this in your client code. var socket = io.connect('http://' + location.host + ':3000');

使用项目的虚拟主机名连接套接字。这个对我有用。在您的客户端代码中编辑它。 var socket = io.connect('http://'+ location.host +':3000');

#1


First make sure that you have installed all dependencies of your server.js file, if you are working with Homestead, execute the following command in your root project directory:

首先确保已安装server.js文件的所有依赖项,如果您正在使用Homestead,请在根项目目录中执行以下命令:

npm install express redis socket.io --save

npm install express redis socket.io --save

Wait until installation finish and start the server with:

等到安装完成并启动服务器:

node server.js

#2


Use your project's virtual host name for connect with socket. It works for me. Edit this in your client code. var socket = io.connect('http://' + location.host + ':3000');

使用项目的虚拟主机名连接套接字。这个对我有用。在您的客户端代码中编辑它。 var socket = io.connect('http://'+ location.host +':3000');