Overv / WebCraft

Minecraft clone written in Javascript.
zlib License
388 stars 188 forks source link

multiplayer Uncaught ReferenceError: io is not defined #10

Open jmarrismoboces opened 8 years ago

jmarrismoboces commented 8 years ago

I am having trouble using this for a multiplayer game. I'm currently using 'python -m http.server' to host the web files, after running 'node server.js' in another command prompt. In multiplayer.html, I receive the above error after I press 'Enter' to submit my user name.

jmarrismoboces commented 8 years ago

Just wanted to add that this also happens when using 'http-server' in the main project directory.

Overv commented 8 years ago

It seems socket.io is never included in the HTML. You'll need to add a script tag after glmatrix to include it. I'm not sure how that happened.

On Mon, Feb 1, 2016, 15:46 jmarrismoboces notifications@github.com wrote:

Just wanted to add that this also happens when using 'http-server' in the main project directory.

— Reply to this email directly or view it on GitHub https://github.com/Overv/WebCraft/issues/10#issuecomment-177999967.

jmarrismoboces commented 8 years ago

Realized socket.io.js was not being served, despite having done npm install socket.io in the project directory. Changed the reference in the multiplayer.html file to refer to the socket.io CDN. I'm now getting 'GET http://localhost:8080/socket.io/?EIO=3&transport=polling&t=LATPEgI 404 (Not Found)' repeatedly, where the t parameter changes each time. I'm a little confused as to how and where the node server should be supplying the socket.io library. It would seem that the client is looking for it at :3000/socket.io/socket.io.js, but when I run npm install socket.io, the files don't seem to end up that way. Here's what I have in node_modules:

... socket.io |- lib | |-client.js | |-index.js | |-namespace.js | |-socket.js socket.io-adapter socket.io-client |- lib | |-index.js | |-manager.js | |-on.js | |-socket.js | |-url.js socket.io-parser ...

It seems socket.io is never included in the HTML.

<script type="text/javascript">         
            var head = document.getElementsByTagName( "head" )[0];
            var script = document.createElement( "script" );
            script.type = "text/javascript";
            script.src = "http://" + location.host + ":3000/socket.io/socket.io.js";
            head.appendChild( script );
        </script>

This seems to be where multiplayer.html loads the socket.js library. I changed it to read

<script type="text/javascript">         
            var head = document.getElementsByTagName( "head" )[0];
            var script = document.createElement( "script" );
            script.type = "text/javascript";
            script.src = "http://" + location.host + ":3000/socket.io/lib/socket.js";
            head.appendChild( script );
        </script>

per the file structure above.

Thank you for your quick response! I like what I see, and hope to use this with my class!

jmarrismoboces commented 8 years ago

Just wanted to clarify that those changes didn't fix my problem. I understand that the socket.io lib should be served over port 3000, but the path seems to be incorrect somehow. The installed version is 1.4.5.

What's strange to me is that once I replace the locally hosted socket.io to the CDN hosted one, the client appears to be attempting to access socket.io on the web port (8000 or 8080 for python or node https-server, respectively) rather than 3000. I'm new to sockets.io and node, so I appreciate any information you can offer.

Overv commented 8 years ago

I'll look into this tomorrow.

Overv commented 8 years ago

Okay, the latest code was completely incompatible with newer versions of Socket.IO. I've fixed the problems and the server will now properly host the socket.io files too.

Run node server and browse to http://localhost:3000/multiplayer.html.

JadedChara commented 4 years ago

Receiving the same issue. I'm not sure how to rectify the situation.