//#!/usr/bin/env node // // WebSocket chat server // Implemented using Node.js // // Requires the websocket module. // // WebSocket and WebRTC based multi-user chat sample with two-way video // calling, including use of TURN if applicable or necessary. // // This file contains the JavaScript code that implements the server-side // functionality of the chat system, including user ID management, message // reflection, and routing of private messages, including support for // sending through unknown JSON objects to support custom apps and signaling // for WebRTC. // // Requires Node.js and the websocket module (WebSocket-Node): // // - http://nodejs.org/ // - https://github.com/theturtle32/WebSocket-Node // // To read about how this sample works: http://bit.ly/webrtc-from-chat // // Any copyright is dedicated to the Public Domain. // http://creativecommons.org/publicdomain/zero/1.0/ "use strict"; var https = require('https'); var fs = require('fs'); var nodeStatic = require('node-static'); var WebSocketServer = require('websocket').server; // Used for managing the text chat user list. var connectionArray = []; var nextID = Date.now(); var appendToMakeUnique = 1; // Output logging information to console function log(text) { var time = new Date(); console.log("[" + time.toLocaleTimeString() + "] " + text); } // If you want to implement support for blocking specific origins, this is // where you do it. Just return false to refuse WebSocket connections given // the specified origin. function originIsAllowed(origin) { return true; // We will accept all connections } // Scans the list of users and see if the specified name is unique. If it is, // return true. Otherwise, returns false. We want all users to have unique // names. function isUsernameUnique(name) { var isUnique = true; var i; for (i=0; i