var readline = require('readline'); var io = require('socket.io-client'); var fs = require('fs'); var colors = require('colors'); var dateFormat = require('dateformat'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: false }); process.stdin.setRawMode(true); readline.emitKeypressEvents(process.stdin); var socket = io("http://192.168.2.188:3000"); var uID; var sessionID; var token; var typingTimer; var typing = false; socket.on('connect', function () { console.log("Connected."); fs.readFile('token', function (err, data) { if (!err) { token = data.toString(); console.log("Sending token: %s".yellow, token); socket.emit("token:validate", token); } else { console.log("Requesting token...".yellow) socket.emit("token:generate"); } }); }); socket.on('disconnect', function () { console.log("Disconnected.".red); if (token) { console.log("Saving token...".yellow); fs.writeFile('token', token, function (err) { console.log("Token saved, exiting...".green); process.exit(); }); } else { console.log("Exiting...".red); process.exit(); } }) socket.on('token:validated', function (data) { console.log("Token validated!".green); console.log("User ID: ", data.id); uID = data.id; }); socket.on('token:generated', function (data) { console.log("Token generated: %s".green, data.token); console.log("User ID: ", data.id); token = data.token; uID = data.id; }); socket.on('chat:start', function (data) { sessionID = data.session_id; console.log("Chat started!".green); console.log(" session id: %s", sessionID); if (data.messages.length > 0) { data.messages.forEach(function (data) { console.log("[%s] %s: %s", data.time, data.client.id === uID ? "You".cyan : "Operator".green, data.message); }); } }); socket.on('chat:end', function (data) { console.log("Chat ended!".red); console.log(" session id: %s", sessionID); socket.emit('chat:rating', sessionID, 4); }); socket.on('chat:archive', function (data) { console.log("Chat archived!".green); console.log(" session id: %s", sessionID); sessionID = undefined; }); socket.on('chat:message', function (data) { console.log("[%s] %s: %s", data.time, data.client.id === uID ? "You".cyan : "Operator".green, data.message); }); socket.on('chat:typing', function (data) { if (data.client.id !== uID) { console.log("[%s] Operator %s typing.", (new Date).toISOString(), data.typing ? "started" : "stopped"); } }); rl.on('line', function (line) { if (sessionID) { /*console.log("Sending message: %s", line); console.log(" session id: %s", sessionID);*/ socket.emit("chat:message", sessionID, line); } else { //console.log("Starting new chat with message: %s", line); socket.emit("chat:create", line); } }); process.stdin.on('keypress', function (str, key) { if (key && key.ctrl && key.name === 'c') { process.emit('SIGINT'); } else if (str !== undefined) { process.stdout.write(str); if (sessionID) { if (!typing) { typing = true; socket.emit("chat:typing", sessionID, true); } clearTimeout(typingTimer); typingTimer = setTimeout(function () { socket.emit("chat:typing", sessionID, false); typing = false; }, 1000); } } }); process.on('SIGINT', function () { socket.emit("chat:typing", sessionID, false); socket.disconnect(); }); socket.connect();