{"size": 2084, "ext": "js", "max_stars_repo_path": "server/server.js", "max_stars_repo_name": "lucasedsonm/space-lizard", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/server.js", "max_issues_repo_name": "lucasedsonm/space-lizard", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/server.js", "max_forks_repo_name": "lucasedsonm/space-lizard", "max_forks_repo_forks_event_min_datetime": "2020-06-27T14:56:26.000Z", "max_forks_repo_forks_event_max_datetime": "2020-06-27T14:56:26.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 28.1621621622, "max_line_length": 89, "alphanum_fraction": 0.613243762}
"use strict";

process.title = "space-lizard-test"

const webSocketsServerPort = 1234
const webSocketServer = require("websocket").server
const http = require("http")

const server = http.createServer(function(request, response) {
    // We don"t need anything from the http server.
});

server.listen(webSocketsServerPort, function() {
    console.log("WebSocket server is listening on port [" + webSocketsServerPort + "].");
})

// Create the websocket server
const wsServer = new webSocketServer({
    httpServer: server
})

//Connect to local version of Redis
//Defaulted to localhost and port 6379
var redis = require('redis')
var redisClient = redis.createClient()

redisClient.on('connect', function () {
    console.log("Redis connected")
})

// Main websocket connection code.
wsServer.on("request", function(request) {
    // accept connection - you should check "request.origin" to
    // make sure that client is connecting from your website
    // (http://en.wikipedia.org/wiki/Same_origin_policy)

    const connection = request.accept(null, request.origin); 
    console.log("User joined your channel!")

    // Say hello to the connected player.
    const data = {
        type: "hello",
        text: `Wow its working! Hi ${request.origin}.`
    }

    // Let's repeatedly send this
    //setInterval(() => connection.sendUTF(JSON.stringify(data)), 100)

    // On receiving a message
    connection.on("message", function(message) {
        if (message.type === "utf8") {
            var dataString = message.utf8Data;
            var data = null;
            if(typeof dataString !== "undefined" && dataString !== null){
                try{
                    data = JSON.parse(dataString);
                }
                catch(ex){}
            }

            if(data === null){
                console.log("Invalid message dataString [" + dataString + "]");
                return;
            }
        }
    })

    // On disconnection.
    connection.on("close", function() {
        // Free up memory?
        console.log("DISCONNECTED    D:")
    })
})
