I've tired the code below it isn't worked:
var WebSocketServer = require('ws').Server;
var wsServer = new WebSocketServer({port: 8080});
wsServer.on('connection', function(ws) {
if (ws.origin != 'http://example.com') {
console.log('Origin was not http://example.com');
return;
}
});
This code is working fine with worlize's websocket server package. But I prefer einaros' better. However, when I investigate ws.property, it contains something like below:
headers: {
...
origin: 'http://example.com'
...
},
So how do I verify the origin of browser request.
Thank you,
I can find it eventually:
var domain = ws.upgradeReq.headers.origin;
Searching from source code I found out a solution that is acceptable for me. Through verifyClient option
var webSockOpts=
{port :myport
,verifyClient : function (info, callback) {
var question=url.parse(info.req.url, true, true);
if (parseInt (question.query.API_KEY) === 123456789) {
status= true; // I'm happy
code = 400; // everything OK
msg = ''; // nothing to add
} else {
status= false; // I'm noy happy
code = 404; // key is invalid
msg = 'Demo requires API_KEY=123456789';
}
callback (status,code,msg);
}
}
wsServer=new webSocket (webSockOpts);
In your HTML/JavaScript add something like:
ws = new WebSocket('ws://' + host + ':4081' + '/log?API_KEY=123456789');