X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=sockets.js;h=11fe91ccc8079e07c91e899c7c62608b1d217fcb;hb=a48ee8b80ae4902f1e70629a608396fe3ff046d0;hp=962e1d8eaa40b233974e2ae3cf3aa7ae1d557d6f;hpb=3a60958052399bcfcdda221de2c3cb8abe7f19ea;p=vchess.git diff --git a/sockets.js b/sockets.js index 962e1d8e..11fe91cc 100644 --- a/sockets.js +++ b/sockets.js @@ -1,6 +1,17 @@ const url = require('url'); const sqlite3 = require('sqlite3'); -const db = new sqlite3.Database('db/vchess.sqlite'); +const db = new sqlite3.Database(__dirname + "/db/vchess.sqlite"); + +// Node version in Ubuntu 16.04 does not know about URL class +function getJsonFromUrl(url) { + var query = url.substr(2); //starts with "/?" + var result = {}; + query.split("&").forEach(function(part) { + var item = part.split("="); + result[item[0]] = decodeURIComponent(item[1]); + }); + return result; +} module.exports = function(wss) { db.serialize(function() { @@ -12,9 +23,12 @@ module.exports = function(wss) { // No-op function as a callback when sending messages const noop = () => { }; wss.on("connection", (socket, req) => { - const params = new URL("http://localhost" + req.url).searchParams; - const sid = params.get("sid"); - const page = params.get("page"); +// const params = new URL("http://localhost" + req.url).searchParams; +// const sid = params.get("sid"); +// const page = params.get("page"); + var query = getJsonFromUrl(req.url); + const sid = query["sid"]; + const page = query["page"]; // Ignore duplicate connections: if (!!clients[page][sid]) { @@ -62,7 +76,7 @@ module.exports = function(wss) { break; case "ping": if (!!clients[page][obj.oppid]) - socket.send(JSON.stringify({code:"pong"})); + socket.send(JSON.stringify({code:"pong",gameId:obj.gameId})); break; case "myname": // Reveal my username to opponent