Jump to content

Can not get this file to work in node.js


Achilles
 Share

Recommended Posts

Recently, I've been trying to understand node.js and socket.io. I've been browsing the internet for tutorials and found what seems to be a very helpful one located here: https://github.com/JoshuaChing/MultiplayerExample

 

He left no instructions for how to install and run. It says he just used node.js and socket.io, so I gave some common attempts.

However, no matter what I do, I can not get it to work. Any suggestions?

Link to comment
Share on other sites

I'm going to assume you have node and npm installed.

 

Clone the repo and hit up `npm init` to create a `package.json`.

 

Looks like the only dependency is socket.io itself, I've not picked through much so I'm going to install the latest, but the project is a little old so we'll see if it still works.

 

```

npm i -S socket.io

```

 

Having installed socket.io you need one more thing as it tries to start the server on a remote url.

 

Open up `server.js` and change the address to `0.0.0.0`, this will make it local, then start the server

 

```

node server.js

```

 

All things being equal the server will start.

 

I'm just looking at why it wont actually server anything, there are a couple of odd bits in there on quick inspection. But, those instructions above will start the project.

 

edit oh, its simple, the server is only for the socket connections, you'll need to serve up the client yourself. As you've node going just install `serve` and let that do it

 

```

npm i -D serve

./node_modules/.bin/serve public

```

 

npm will work the same on windows, I'm just not sure how you execute the `node_modules/.bin/serve` if youre not in a bash (or similar) shell.

 

`serve` will default to port 3000 so point your browser to `0.0.0.0:3000` and it all works. Well, when I say works, it loads up, not sure the code is working quite right though.

 

As a disclaimer, I've just written a `socket.io` wrapper for the `Koa` framework (which is node) so I can attest for how easy socket.io is to get up and running. Its a very good abstraction around handling websockets.

Link to comment
Share on other sites

I have a decent amount of experience with Node.js. That being said, I tried everything you did above, and could not get it to work. I even changed the server to go to localhost. Here's what it tells me:

 

ee1e40d3ee5deb93600f.png?raw

 

Yet when I go to localhost:3000, I get an error code (http://achill.es/img/479d24c48ef54afdf5d5.png?raw).

I'm starting to think there maybe something wrong with the code. Did you actually get it to work?

Link to comment
Share on other sites

Yeps, got it working, well, by working I mean it looked like the screenshot, didnt seem to do much and I didnt look why, no errors thrown though.

 

The `node server` is just for `socket.io` connections, it doesnt serve any html or other files.

 

You'll need to serve the public directory some other way, firing up the node module `serve` would do that but you probably have a go-to method for serving files on your machine anyway.

 

The socket.io docs on their site is the place to go though, the chat example is super simple and easy to understand.

 

I'm not sure if this will help but my library for koa and socket.io is pretty lightweight, maybe helpful?

Link to comment
Share on other sites

Yeps, got it working, well, by working I mean it looked like the screenshot, didnt seem to do much and I didnt look why, no errors thrown though.

 

The `node server` is just for `socket.io` connections, it doesnt serve any html or other files.

 

You'll need to serve the public directory some other way, firing up the node module `serve` would do that but you probably have a go-to method for serving files on your machine anyway.

 

The socket.io docs on their site is the place to go though, the chat example is super simple and easy to understand.

 

I'm not sure if this will help but my library for koa and socket.io is pretty lightweight, maybe helpful?

 

I used the awesome chat tutorial on the socket.io website, very helpful. However, this one is more gaming related, so I really wanted to get it to work. Unfortunately, it just isn't working for me. Anything else you did to get it to work?

Link to comment
Share on other sites

Nope, literally installed deps, changed the server address, started the server, served public directory

 

I'm on a Mac but as this is node and just starting a server the steps will be identical for Windows.

 

HpTDet1.png

 

Theres a 2nd URL in the client.js, make sure that points to the your connection server as well. Other than that its actually working fine, I get updates when I set up a new connection (i.e. point a new tab to the url) and I can move around with WASD. Its a very simple implementation that gets the job done.

 

I'm actually looking to do a quick write-up on the socket module I created, I just want to create a small game first to accompany it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...