Laravel中的Socket.io轮询404 [英] Socket.io polling 404 in Laravel

查看:496
本文介绍了Laravel中的Socket.io轮询404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Socket.io
在我的Laravel应用程序中实现一个聊天应用程序。聊天应用程序可以自己工作,
,但我遇到问题,使其在Laravel工作。



我尝试在8000端口上服务Laravel聊天服务器在8000.
我使用Express 4.8.0和Socket.io 1.0.6,节点0.10.29和nodemon进行测试。



//服务器.js:

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

http.listen(8000,function(){
console.log('listen on *:8000');
});

app.use('/',express.static(__ dirname +'/ public'));
app.get(/ *,function(req,res){
res.sendFile(__ dirname +/index.php);
});

// client.js:

  var socket = io.connect('http:// localhost:8000'); 

// html - 依赖关系,我尝试了所有这些:

 < script src =// cdn.socket.io/socket.io-1.0.0.js\"></script> 
{{HTML :: script('/ socket.io/socket.io.js')}}
< script src =http:// localhost:8000 / socket.io / socket。 io.js>< / script>
< script src ={{asset('/ socket.io/socket.io.js')}}\"\"gt ;</script>

然后为客户端(自己的代码)

  {{HTML :: script('js / client.js')}} 

Socket.io的CDN版本不断提供这些类型的日志:

 GET http :// localhost:8000 / socket.io /?EIO = 2& transport = polling& t = 1407425555977-15 404(未找到)。 

其他的只是给出一个没有找到的js文件日志:

 GET http:// localhost:8000 / socket.io / socket.io.js 404(未找到)

//文件夹结构:


/ public




/ js


client.js


/ node_modules



server.js


< blockquote>

任何人都可以看到我能做些什么来使其工作?



编辑
//server.js

  var socket = require('socket.io') ; 
var express = require('express');
var http = require('http');

var app = express();
var server = http.createServer(app);

var io = socket.listen(server);

io.on('connection',function(socket){
console.log(Connected server);
}

服务器。听(8000);

// client.js



$ pre $ var socket;
$(document).ready(function(){
socket = io.connect('http:// localhost: );
});

//当我在全局日志说:

  connected:false 
已断开连接:true


解决方案

这是因为你设置不正确我有同样的确切问题(同样的错误和基本代码布局)您需要在页面的基本目录(与index.php文件所在的位置相同)中执行 npm install socket.io --save )然后你必须为express( npm install express --save )执行相同的操作,你也必须更改你的服务器代码。 / p>

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

To:

 code> var socket = require('socket.io'); 
var express = require('express');
var http = require('http');

var app = express();
var server = http.createServer(app);

var io = socket.listen(server);

然后删除 app.use app.get ,因为它们不再需要如何完成。然后在server.js的末尾添加 server.listen(8000); 。对于依赖关系,请使用:< script src =// cdn.socket.io/socket.io-1.0.0.js\"></script> 。然后,要运行您的服务器,请在终端中进行操作,然后键入 node server.js 。然后连接到您的客户端。另外,对于事件,在服务器中使用:

  io.on('connection',function(client){
client.on('someEvent',function(someVariables){
//当客户端发出'someEvent'时,使用someVariable执行某些操作
io.emit('anEventToClients',someData);
));
client.on('anotherEvent',function(someMoreVariables){
//当客户端发出anotherEvent时,使用someMoreVariables执行更多操作
io.emit('anotherEventToClients' ,someMoreData);
});
});

在您的客户端代码中:

  socket.emit('someEvent',variables); 
socket.on('anEventToClients',function(something){
//当从服务器发出anEventToClient时的代码
});


I am trying to implement a chat app with Socket.io in to my Laravel app. The chat app works fine on it's own, but I am having problems to make it work in Laravel.

I try to serve Laravel on port 8000 and the chat server on 8000. I use Express 4.8.0 and Socket.io 1.0.6, Node 0.10.29 and nodemon for testing.

//server.js:

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

http.listen(8000, function () {
 console.log('listening on *:8000');
});

app.use('/', express.static(__dirname + '/public'));
app.get("/*", function (req, res){
 res.sendFile(__dirname + "/index.php");
});

//client.js:

var socket            = io.connect('http://localhost:8000');

//html - dependencies, I tried all these:

<script src="//cdn.socket.io/socket.io-1.0.0.js"></script>
{{ HTML::script('/socket.io/socket.io.js') }}
<script src="http://localhost:8000/socket.io/socket.io.js" ></script>
<script src="{{asset('/socket.io/socket.io.js')}}"></script>

and then for the client side (own code)

{{ HTML::script('js/client.js') }}

The CDN version of Socket.io gives constantly these kinds of logs:

"GET http://localhost:8000/socket.io/?EIO=2&transport=polling&t=1407425555977-15 404 (Not Found)".

The others ones just gives a js file not found log:

"GET http://localhost:8000/socket.io/socket.io.js 404 (Not Found)"

//folder structure:

/public

/js

client.js

/node_modules

server.js

Can anyone see what I can do to make it work?

EDIT //server.js

var socket  = require('socket.io');
var express = require('express');
var http    = require('http');

var app     = express();
var server  = http.createServer(app);

var io      = socket.listen(server);

io.on('connection', function (socket) {
 console.log("Connected server");
}

server.listen(8000);

//client.js

var socket;
$(document).ready(function () {
 socket            = io.connect('http://localhost:8000');
});

//When I typ the global "socket" object in the log it says:

connected: false
disconnected: true

解决方案

This is because you have set it up incorrectly. I had the same exact problem you did (same errors and basic code layout). You need to do npm install socket.io --save while in the base directory of your page (the same as where your index.php file is located). Then you have to do the same for express (npm install express --save). You also have to change your server code. Change the creation of io from:

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

To:

var socket = require('socket.io');
var express = require('express');
var http = require('http');

var app = express();
var server = http.createServer(app);

var io = socket.listen(server);

Then remove the app.use and app.get as they are no longer needed for how this is going to be done. Then add server.listen(8000); at the end of the server.js. For dependencies, use: <script src="//cdn.socket.io/socket.io-1.0.0.js"></script>. Then, to run your server, go to it in terminal and type node server.js. Then just connect to it with your client. Also, for events, in the server, use:

io.on('connection', function (client) {
    client.on('someEvent', function(someVariables){
        //Do something with someVariables when the client emits 'someEvent'
        io.emit('anEventToClients', someData);
    });
    client.on('anotherEvent', function(someMoreVariables){
        //Do more things with someMoreVariables when the client emits 'anotherEvent'
        io.emit('anotherEventToClients', someMoreData);
    });
});

And in your client code:

socket.emit('someEvent', variables);
socket.on('anEventToClients', function(something){
    //Code when anEventToClient is emitted from the server
});

这篇关于Laravel中的Socket.io轮询404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆