Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get socket.io.js

I'm actually working on a little project, where i'm supposed to recreate a drawing multiplayer game with node.js, mongoDB, socket.io and canvas.

The drawer is working like a charm, and the server seems to work well too. I got my register/login/sessions and database up and working, the only problem is socket.io. When an user is joining the game room, he can see the drawer and tools, but no connection. Why ? The browser can't find socket.io.js.

What I did :

I verified if it was installed, it is with npm install socket.io. I checked if the server was starting it when turning the server on : Got "socket.io started" in my console. I checked my HTML code, here it is :

<script type="text/javascript" src="/socket.io/socket.io.js"></script>

According to the billions of tutorials/dev sites/help subjects, this is supposed to work. But it's not. When opening the console of my browser, I got this :

X GET http://localhost:1337/socket.io/socket.io.js NOT FOUND.

I don't know where is the problem, I can't figure this out and it's giving me a huge headache.. So I'm here.

Thanks in advance for helping ! :)

like image 420
YumeYume Avatar asked Jan 26 '14 15:01

YumeYume


People also ask

Where is Socket.IO in js?

If you would like to use the local version of the client-side JS file, you can find it at node_modules/socket.io/client-dist/socket.io.js .

How do I start Socket.IO in node JS?

In order to do it, you need to create an index. js file and install socket.io and express. You can use the following command: touch index. js && npm install express socket.io && npm install --save-dev nodemon .


1 Answers

Given the code in your comment, you're not using the correct variable for initializing socket.io.

Try this:

var express = require('express');
var app     = express();
var server  = app.listen(1337);
var io      = require('socket.io').listen(server);
...

So instead of having socket.io 'listen' on the Express app instance, it should listen to what app.listen(...) returns (which happens to be an http.Server instance).

like image 196
robertklep Avatar answered Oct 14 '22 00:10

robertklep