08-02-2012 03:15 PM
Hello everyone. I'm started messing around with node.js and am trying to setup a basic web socket app. I've installed node.js for Windows, WAMP, and am using the node websocket lib. When I run 'node server.js' and is successful and when I connect to 'localhost/client/index.htm' in a webbrowser the server console acknowledges there was a connection and gives the connection ID but in the developer tools of the browser (Chrome) I get the error:
Error: Firefox can't establish a connection to the server at ws://127.0.0.1:8000/.
Source File: http://localhost/client/js/html5games.websocket.js
Line: 9
var websocketGame = {
}
// init script when the DOM is ready.
$(function(){
// check if existence of WebSockets in browser
if (window["WebSocket"]) {
// create connection
websocketGame.socket = new WebSocket("ws://127.0.0.1:8000");
// on open event
websocketGame.socket.onopen = function(e) {
console.log('WebSocket connection established.');
};
// on message event
websocketGame.socket.onmessage = function(e) {
console.log(e.data);
};
// on close event
websocketGame.socket.onclose = function(e) {
console.log('WebSocket connection closed.');
};
}
});
the client index.htm is:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>WebSockets demo for HTML5 Games Development: A Beginner's Guide</title> <meta name="description" content="This is a WebSockets demo for the book HTML5 Games Development: A Beginner's Guide by Makzan"> <meta name="author" content="Makzan"> </head> <body> <script src="js/jquery-1.7.2.min.js"></script> <script src="js/html5games.websocket.js"></script> </body> </html>
And finally the server.js
var ws = require(__dirname + '/lib/ws/server');
var server = ws.createServer();
var totalConnectedClients = server.manager.length;
server.addListener("connection", function(conn){
// init stuff on connection
console.log("A connection established with id",conn.id);
var message = "Welcome "+conn.id+" joining the party. Total connection:"+server.manager.length;
server.broadcast(message);
});
server.listen(8000);
console.log("WebSocket server is running.");
console.log("Listening to port 8000.");
var message = "a message from server";
server.broadcast(message);
I have no idea why the client/server hsndshake won't complete, the server acknowledges a connection but immediately drops it, but the client file's debug says it couldn't connect. What's going on here?
08-14-2012 01:20 PM
This doc may be old, but does have a clear description of each of the different client/server pieces.
http://zackhobson.com/2010/03/28/node-js-and-web-s
Looking at your code snippet, I don't see the data or close events in your server.js file. Could adding a listener for ondata help get where you want to be?
08-14-2012 02:30 PM