I am quite new in webSocket node js. I was trying to making a qrcode login and the best method i found was using websocket. Actually i have known and have successful run the webscoket using Node.js
.
Too bad i only can use the Node.js
in my localhost only so it would always run in http:localhost:700
what i want expected are:
example i have a website with https://www.example.com
i want the webscoket server could be run in the server and could be accessed by the client based on my website server
not only can be accessed on my web server only
this is my server.js :
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
var ip = '';
var port1 = 700;
var port2 = 701;
var WebSocketServer = require('ws').Server
var uuid = require('node-uuid');
var wss = new WebSocketServer({ port: port1,server:ip });
var clients = {};
var dumCounter=0;
wss.on('connection', function connection(ws) {
console.log(ip);
ws.on('message', function incoming(message) {
console.log('received: %s', message);
var obj = JSON.parse(message);
if(obj.op == 'qrcode')
{
var uuidToken = uuid.v1();
clients[uuidToken] = ws;
var hello = { op:'qrcode',token:uuidToken};
ws.send(JSON.stringify(hello),{mask:false});
}
});
});
wss.onclose = function(event) {
log('Closed connection ');
};
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain","Access-Control-Allow-Origin":"*"});
process.on('uncaughtException', function(err) {
response.end("Exception");
});
if(request.method == "POST")
{
var url = request.url;
if(url == "/auth")
{
var body = '';
request.on('data', function(chunk)
{
body += chunk.toString();
});
request.on('end', function () {
var params = JSON.parse(body);
var uuId = params.uuid;
var accessToken = params.user_id;
var msg = {'op':'authdone','userId':accessToken};
if(clients[uuId] != undefined || clients[uuId] != null)
{
// console.log("Before "+Object.size(clients));
clients[uuId].send(JSON.stringify(msg),{mask:false});
delete clients[uuId];
// console.log("After "+Object.size(clients));
response.end('{"status":"OK"}');
}
else
{
// console.log("Recived Params: "+JSON.stringify(params));
response.end('{"status":"NOK1"}');
}
});
}
else
{
response.end('{"status":"NOK2"}');
}
}
else
{
response.end("NOT Supported");
}
}).listen(port2,ip);
when i use Remote Dekstop Connection
and run the websocket using npm start
than open my website using remote browser
i could run the websocket. But when i use my browser i cant see the websocket is working. Also i try open my ip:700 example 123.123.123.123:700
it is working on remote dekstop but not in my browser
can you help me ?
via Kelvin
No comments:
Post a Comment