I have build a game using canvas and Javascript, and I would like to implement multiplayer functionality using WebSockets and Node.js
I'm pletely new to Node, and I have managed to get a basic web server up and running with the following code:
var http = require("http");
console.log("Server started at port 8888");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
It all works, I get the response of "Hello World" when I navigate to to my server IP on port 8888. My question is, is this all I need to start using WebSockets with Node? I have heard people say I still need socket.io so that Node can use sockets, but I don't know if that's just a library to help me use sockets or if Node actually can't understand sockets.
The server basically has to keep a note of all players connected, their scores, their positions on the canvas, etc. A client will poll the server (using WebSockets) occasionally in order to get everyones positions and then update their canvas with the returned information. Would I need socket.io for this? Either way, how would I go about doing this?
Thanks.
I have build a game using canvas and Javascript, and I would like to implement multiplayer functionality using WebSockets and Node.js
I'm pletely new to Node, and I have managed to get a basic web server up and running with the following code:
var http = require("http");
console.log("Server started at port 8888");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
It all works, I get the response of "Hello World" when I navigate to to my server IP on port 8888. My question is, is this all I need to start using WebSockets with Node? I have heard people say I still need socket.io so that Node can use sockets, but I don't know if that's just a library to help me use sockets or if Node actually can't understand sockets.
The server basically has to keep a note of all players connected, their scores, their positions on the canvas, etc. A client will poll the server (using WebSockets) occasionally in order to get everyones positions and then update their canvas with the returned information. Would I need socket.io for this? Either way, how would I go about doing this?
Thanks.
Share Improve this question asked Nov 12, 2011 at 23:31 James DawsonJames Dawson 5,40920 gold badges75 silver badges129 bronze badges2 Answers
Reset to default 4Two weeks ago, I put on my blog tutorial on Node.js & WebSocket - Simple chat tutorial.
Basically, I took this WebSocket module for Node.js and just extended this server template (no socket.io):
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
// process HTTP request. Since we're writing just WebSockets server
// we don't have to implement anything.
});
server.listen(1337, function() { });
// create the server
wsServer = new WebSocketServer({
httpServer: server
});
// WebSocket server
wsServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
// This is the most important callback for us, we'll handle
// all messages from users here.
connection.on('message', function(message) {
if (message.type === 'utf8') {
// process WebSocket message
}
});
connection.on('close', function(connection) {
// close user connection
});
});
If you're developing multiplayer game you might be interested in talk HTML5 Games with Rob Hawkes of Mozilla (he describes his experience with developing multiplayer game based on WebSocket) and also article Developing Multiplayer HTML5 Games with Node.js (there's a paragraph about sharing JavaScripts among Node.js and frontend).
You need socket-io because that's a library built on top of node js. Socket io create's a "socket" so data can be transferred from a web app to server or vice versa. node js is just a javascript interface to your server.
there's information on how to setup socket-io on:
Installing/setting up Socket.IO on my server