javascript - Websockets with Node -
i'm having trouble trying play websockets, made toy echo server , tried send requests directly browser console. can send message browser server, , server recieving properly, it's not sending echo back. here server code , browser console code i'm running little test:
server.js
var websocketserver = require('websocket').server; var http = require('http'); var server = http.createserver(function(request, response) { console.log((new date()) + ' received request ' + request.url); response.writehead(404); response.end(); }); server.listen(8080, function() { console.log((new date()) + ' server listening on port 8080'); }); wsserver = new websocketserver({ httpserver: server }); wsserver.on('request', function(request) { var connection = request.accept('echo-protocol', request.origin); console.log((new date()) + ' connection accepted.'); connection.on('message', function(message) { console.log('received message: ' + message.utf8data); connection.sendutf("message test"); }); connection.on('close', function(reasoncode, description) { console.log((new date()) + ' peer ' + connection.remoteaddress + ' disconnected.'); }); });
firefox console input:
var connection = new websocket('ws://localhost:8080', 'echo-protocol'); // undefined // http://localhost:8080/ connection.onmessage = function(mess) {console.log(mess.data);}; // function connection.onmessage() connection.send("hello"); // undefined
command prompt input , output:
> node server.js fri oct 09 2015 22:57:18 gmt-0700 (pacific daylight time) server listening on port 8080 fri oct 09 2015 22:57:21 gmt-0700 (pacific daylight time) connection accepted. received message: hello
i did npm install websocket
before making toy server, if problem lies in websocket library i'm using, that's one
my server gets @ least line 22 since prints out message sent browser, browser console doesnt show response when message supposedly sent on next line sendutf function.
am doing wrong? browser silently dropping response?
i hope it's easy (for sake)...
in firefox browser console, on "logging" drop-down, click "log". exact code seemed fail on machine in firefox, yet worked in chrome. found option in firefox console , saw message test
output.
Comments
Post a Comment