Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- // Optional. You will see this name in eg. 'ps' or 'top' command
- process.title = 'node-chat';
- // Port where we'll run the websocket server
- const webSocketsServerPort = 1337;
- // websocket and http servers
- const webSocketServer = require('websocket').server;
- const http = require('http');
- const fs = require('fs');
- const path = require('path');
- /**
- * Global variables
- */
- // latest 100 messages
- var history = [ ];
- // list of currently connected clients (users)
- var clients = [ ];
- /**
- * Helper function for escaping input strings
- */
- function htmlEntities(str) {
- return String(str).replace(/&/g, '&').replace(/</g, '<')
- .replace(/>/g, '>').replace(/"/g, '"');
- }
- function shuffle(array) {
- let counter = array.length;
- while (counter > 0) {
- const index = Math.floor(Math.random() * counter);
- counter--;
- const temp = array[counter];
- array[counter] = array[index];
- array[index] = temp;
- }
- return array;
- }
- // Array with some colors
- var colors = shuffle([ 'red', 'green', 'blue', 'magenta', 'purple', 'plum', 'orange' ]);
- /**
- * HTTP server
- */
- var server = http.createServer(function(request, response) {
- // Only support two things, send frontend.html and chat-frontend.js
- let filePath = '.' + request.url;
- if (filePath == './') {
- filePath = './frontend.html';
- }
- let contentType = 'text/html';
- if (path.extname(filePath) == '.js') {
- contentType = 'text/javascript';
- }
- fs.readFile(filePath, function(error, content) {
- response.writeHead(200, { 'Content-Type' : contentType });
- response.end(content, 'utf-8');
- });
- });
- server.listen(webSocketsServerPort, function() {
- console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
- });
- /**
- * WebSocket server
- */
- const wsServer = new webSocketServer({
- // WebSocket server is tied to a HTTP server. WebSocket request is just
- // an enhanced HTTP request. For more info http://tools.ietf.org/html/rfc6455#page-6
- httpServer: server
- });
- // This callback function is called every time someone
- // tries to connect to the WebSocket server
- wsServer.on('request', function(request) {
- console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
- // 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);
- // we need to know client index to remove them on 'close' event
- const index = clients.push(connection) - 1;
- let userName = false;
- let userColor = false;
- console.log((new Date()) + ' Connection accepted.');
- // send back chat history
- if (history.length > 0) {
- connection.sendUTF(JSON.stringify( { type: 'history', data: history} ));
- }
- // user sent some message
- connection.on('message', function(message) {
- if (message.type === 'utf8') { // accept only text
- if (userName === false) { // first message sent by user is their name
- // remember user name
- userName = htmlEntities(message.utf8Data);
- // get random color and send it back to the user
- userColor = colors.shift();
- connection.sendUTF(JSON.stringify({ type:'color', data: userColor }));
- console.log((new Date()) + ' User is known as: ' + userName
- + ' with ' + userColor + ' color.');
- } else { // log and broadcast the message
- console.log((new Date()) + ' Received Message from '
- + userName + ': ' + message.utf8Data);
- send_msg(message, userName, userColor);
- }
- }
- });
- // user disconnected
- connection.on('close', function(connection) {
- if (userName !== false && userColor !== false) {
- console.log((new Date()) + " Peer "
- + connection.remoteAddress + " disconnected.");
- // remove user from the list of connected clients
- clients.splice(index, 1);
- // push back user's color to be reused by another user
- colors.push(userColor);
- send_msg({ utf8Data: "*** disconnected from server ***" }, userName, userColor)
- }
- });
- });
- function send_msg(message, userName, userColor) {
- const obj = {
- time: (new Date()).getTime(),
- text: htmlEntities(message.utf8Data),
- author: userName,
- color: userColor
- };
- // we want to keep history of all sent messages
- history.push(obj);
- history = history.slice(-100);
- // broadcast message to all connected clients
- const json = JSON.stringify({ type: 'message', data: obj });
- for (var i = 0; i < clients.length; i++) {
- clients[i].sendUTF(json);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment