Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
RapsFan
Posts: 442
Registered: ‎08-02-2010
My Device: Z10

Looking for tips on development with websockets in Windows

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 during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing :1
WebSocket connection closed.
In Firefox I get:

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

the html5games.websocket.js code is 
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?

Please use plain text.
BlackBerry Development Advisor (Retired)
astanley
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Looking for tips on development with websockets in Windows

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-sockets.html

 

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?

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Please use plain text.
Developer
RapsFan
Posts: 442
Registered: ‎08-02-2010
My Device: Z10

Re: Looking for tips on development with websockets in Windows

Thanks for that site. Will try what you suggested and let you know.
Please use plain text.