简体   繁体   中英

Socket.io polling 404 in Laravel

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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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