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() {
// 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])
{
let obj = JSON.parse(objtxt);
switch (obj.code)
{
+ case "newchat":
+ if (!!clients[page][obj.oppid])
+ {
+ clients[page][obj.oppid].send(
+ JSON.stringify({code:"newchat",msg:obj.msg}), noop);
+ }
+ break;
case "newmove":
if (!!clients[page][obj.oppid])
{
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
+ if (!!clients[page][obj.oppid])
+ {
+ clients[page][obj.oppid].send(JSON.stringify({
+ code:"oppname", name:obj.name}));
+ }
break;
case "lastate":
if (!!clients[page][obj.oppid])