Yelon Chat - Guest Client

From Crys, 8 Years ago, written in JavaScript, viewed 589 times.
URL https://paste.godclan.hu/view/ASouSBWQ Embed
Download Paste or View Raw
  1. var readline = require('readline');
  2. var io = require('socket.io-client');
  3. var fs = require('fs');
  4. var colors = require('colors');
  5. var dateFormat = require('dateformat');
  6.  
  7. var rl = readline.createInterface({
  8.     input: process.stdin,
  9.     output: process.stdout,
  10.     terminal: false
  11. });
  12. process.stdin.setRawMode(true);
  13. readline.emitKeypressEvents(process.stdin);
  14. var socket = io("http://192.168.2.188:3000");
  15. var uID;
  16. var sessionID;
  17. var token;
  18. var typingTimer;
  19. var typing = false;
  20.  
  21. socket.on('connect', function () {
  22.     console.log("Connected.");
  23.     fs.readFile('token', function (err, data) {
  24.         if (!err) {
  25.             token = data.toString();
  26.             console.log("Sending token: %s".yellow, token);
  27.             socket.emit("token:validate", token);
  28.         }
  29.         else {
  30.             console.log("Requesting token...".yellow)
  31.             socket.emit("token:generate");
  32.         }
  33.     });
  34. });
  35.  
  36. socket.on('disconnect', function () {
  37.     console.log("Disconnected.".red);
  38.     if (token) {
  39.         console.log("Saving token...".yellow);
  40.         fs.writeFile('token', token, function (err) {
  41.             console.log("Token saved, exiting...".green);
  42.             process.exit();
  43.         });
  44.     }
  45.     else {
  46.         console.log("Exiting...".red);
  47.         process.exit();
  48.     }
  49. })
  50.  
  51. socket.on('token:validated', function (data) {
  52.     console.log("Token validated!".green);
  53.     console.log("User ID: ", data.id);
  54.     uID = data.id;
  55. });
  56.  
  57. socket.on('token:generated', function (data) {
  58.     console.log("Token generated: %s".green, data.token);
  59.     console.log("User ID: ", data.id);
  60.     token = data.token;
  61.     uID = data.id;
  62. });
  63.  
  64. socket.on('chat:start', function (data) {
  65.     sessionID = data.session_id;
  66.     console.log("Chat started!".green);
  67.     console.log("  session id: %s", sessionID);
  68.     if (data.messages.length > 0) {
  69.         data.messages.forEach(function (data) {
  70.             console.log("[%s] %s: %s", data.time, data.client.id === uID ? "You".cyan : "Operator".green, data.message);
  71.         });
  72.     }
  73. });
  74.  
  75. socket.on('chat:end', function (data) {
  76.     console.log("Chat ended!".red);
  77.     console.log("  session id: %s", sessionID);
  78.     socket.emit('chat:rating', sessionID, 4);
  79. });
  80.  
  81. socket.on('chat:archive', function (data) {
  82.     console.log("Chat archived!".green);
  83.     console.log("  session id: %s", sessionID);
  84.     sessionID = undefined;
  85. });
  86.  
  87. socket.on('chat:message', function (data) {
  88.     console.log("[%s] %s: %s", data.time, data.client.id === uID ? "You".cyan : "Operator".green, data.message);
  89. });
  90.  
  91. socket.on('chat:typing', function (data) {
  92.     if (data.client.id !== uID) {
  93.         console.log("[%s] Operator %s typing.", (new Date).toISOString(), data.typing ? "started" : "stopped");
  94.     }
  95. });
  96.  
  97. rl.on('line', function (line) {
  98.     if (sessionID) {
  99.         /*console.log("Sending message: %s", line);
  100.         console.log("  session id: %s", sessionID);*/
  101.         socket.emit("chat:message", sessionID, line);
  102.     }
  103.     else {
  104.         //console.log("Starting new chat with message: %s", line);
  105.         socket.emit("chat:create", line);
  106.     }
  107. });
  108.  
  109. process.stdin.on('keypress', function (str, key) {
  110.     if (key && key.ctrl && key.name === 'c') {
  111.         process.emit('SIGINT');
  112.     }
  113.     else if (str !== undefined) {
  114.         process.stdout.write(str);
  115.         if (sessionID) {
  116.             if (!typing) {
  117.                 typing = true;
  118.                 socket.emit("chat:typing", sessionID, true);
  119.             }
  120.             clearTimeout(typingTimer);
  121.             typingTimer = setTimeout(function () {
  122.                 socket.emit("chat:typing", sessionID, false);
  123.                 typing = false;
  124.             }, 1000);
  125.         }
  126.     }
  127. });
  128.  
  129. process.on('SIGINT', function () {
  130.     socket.emit("chat:typing", sessionID, false);
  131.     socket.disconnect();
  132. });
  133.  
  134. socket.connect();

Replies to Yelon Chat - Guest Client rss

Title Name Language When
Re: Yelon Chat - Guest Client Obese Monkey javascript 8 Years ago.

Reply to "Yelon Chat - Guest Client"

Here you can reply to the paste above