简体   繁体   English

socket.io轮询返回Laravel 500 NotFoundHttpException

[英]socket.io polling returns Laravel 500 NotFoundHttpException

I'm trying to setup a node.js socket.io server, and it's not working. 我正在尝试设置一个node.js socket.io服务器,但它不起作用。 It seems like it's trying to poll to the node.js server first, but because I'm using Laravel for the majority of my app, that's picking it up and returning a Not FoundHttpException 似乎它正在尝试首先轮询node.js服务器,但是由于我将Laravel用于大多数应用程序,因此将其Not FoundHttpException并返回Not FoundHttpException

Node.js server code: Node.js服务器代码:

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

var io = require('socket.io')(server);

var redis = require('ioredis');
var Redis = new redis();

Redis.subscribe('live-updates');

Redis.on('message', function(channel, message) {
    message = JSON.parse(message);

    console.log(channel, message);
    io.emit('foo', message.data);
});

io.on('connection', function() {
    console.log('foo');
});

Client code: 客户代码:

var socket = io.connect('http://myapp.app');

socket.on('foo', function(data) {
    console.log(data);
});

All this does is triggers the following HTTP long polling requests in my client: 这一切都是在客户端触发以下HTTP长轮询请求:

http://myapp.app/socket.io/?EIO=3&transport=polling&t=1447468676627-0

which responds with: 响应:

Server 500 error, #29 Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException in /home/vagrant/myapp/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:161

Well of course it's not found. 好吧,当然没有找到。 I'm expecting socket.io to attempt to create a websocket connection - not send long-polling HTTP requests to an undefined Laravel route. 我期望socket.io尝试创建websocket连接-不将长时间轮询的HTTP请求发送到未定义的Laravel路由。 What am I doing wrong? 我究竟做错了什么?

You don't actually need express here, as you are not displaying anything, just simple http server. 您实际上不需要在这里表达,因为您什么也不显示,只是简单的http服务器。 I think your issue, that the server does not listen. 我认为您的问题是服务器不监听。

Also node should listen to different port than 80, otherwise your http server will handle the request and lead to that 500 error. 另外,节点应该侦听与80不同的端口,否则您的http服务器将处理该请求并导致该500错误。

Check code below. 检查下面的代码。

Server side 服务器端

var app = require('http').createServer(handler);
var io = require('socket.io')(app);

app.listen(3000);

Client side 客户端

var socket = io('http://localhost:3000');

Make sure you run node from CLI 确保您从CLI运行节点

node index.js

And replace index.js with your script name. 并用您的脚本名称替换index.js

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

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