Advertisement
Guest User

Untitled

a guest
May 5th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. var net = require("net");
  2. var sys = require("sys");
  3. var EventEmitter = require("events").EventEmitter;
  4.  
  5. /*******************************************************************************
  6. * ChatServer
  7. *
  8. * Manages connections, users, and chat messages.
  9. ******************************************************************************/
  10.  
  11. function ChatServer() {
  12. this.chatters = {};
  13. this.server = net.createServer(this.handleConnection.bind(this));
  14. this.server.listen(1212, "localhost");
  15. }
  16.  
  17. ChatServer.prototype.isNicknameLegal = function(nickname) {
  18. // A nickname may contain letters or numbers only,
  19. // and may only be used once.
  20. if(nickname.replace(/[A-Za-z0-9]*/, '') != "") {
  21. return false
  22. }
  23. for(used_nick in this.chatters) {
  24. if(used_nick == nickname) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. };
  30.  
  31. ChatServer.prototype.handleConnection = function(connection) {
  32. console.log("Incoming connection from " + connection.remoteAddress);
  33. connection.setEncoding("utf8");
  34.  
  35. var chatter = new Chatter(connection, this);
  36. chatter.on("chat", this.handleChat.bind(this));
  37. chatter.on("join", this.handleJoin.bind(this));
  38. chatter.on("leave", this.handleLeave.bind(this));
  39. };
  40.  
  41. ChatServer.prototype.handleChat = function(chatter, message) {
  42. this.sendToEveryChatterExcept(chatter, chatter.nickname + ": " + message);
  43. };
  44.  
  45. ChatServer.prototype.handleJoin = function(chatter) {
  46. console.log(chatter.nickname + " has joined the chat.");
  47. this.sendToEveryChatter(chatter.nickname + " has joined the chat.");
  48. this.addChatter(chatter);
  49. };
  50.  
  51. ChatServer.prototype.handleLeave = function(chatter) {
  52. console.log(chatter.nickname + " has left the chat.");
  53. this.removeChatter(chatter);
  54. this.sendToEveryChatter(chatter.nickname + " has left the chat.");
  55. };
  56.  
  57. ChatServer.prototype.addChatter = function(chatter) {
  58. this.chatters[chatter.nickname] = chatter;
  59. };
  60.  
  61. ChatServer.prototype.removeChatter = function(chatter) {
  62. delete this.chatters[chatter.nickname];
  63. };
  64.  
  65. ChatServer.prototype.sendToEveryChatter = function(data) {
  66. for(nickname in this.chatters) {
  67. this.chatters[nickname].send(data);
  68. }
  69. };
  70.  
  71. ChatServer.prototype.sendToEveryChatterExcept = function(chatter, data) {
  72. for(nickname in this.chatters) {
  73. if(nickname != chatter.nickname) {
  74. this.chatters[nickname].send(data);
  75. }
  76. }
  77. };
  78.  
  79. /*******************************************************************************
  80. * Chatter
  81. *
  82. * Represents a single user/connection in the chat server.
  83. ******************************************************************************/
  84.  
  85. function Chatter(socket, server) {
  86. EventEmitter.call(this);
  87.  
  88. this.socket = socket;
  89. this.server = server;
  90. this.nickname = "";
  91. this.lineBuffer = new SocketLineBuffer(socket);
  92.  
  93. this.lineBuffer.on("line", this.handleNickname.bind(this));
  94. this.socket.on("close", this.handleDisconnect.bind(this));
  95.  
  96. this.send("Welcome! What is your nickname?");
  97. };
  98.  
  99. sys.inherits(Chatter, EventEmitter);
  100.  
  101. Chatter.prototype.handleNickname = function(nickname) {
  102. if(server.isNicknameLegal(nickname)) {
  103. this.nickname = nickname;
  104. this.lineBuffer.removeAllListeners("line");
  105. this.lineBuffer.on("line", this.handleChat.bind(this));
  106. this.send("Welcome to the chat, " + nickname + "!");
  107. this.emit("join", this);
  108. } else {
  109. this.send("Sorry, but that nickname is not legal or is already in use!");
  110. this.send("What is your nickname?");
  111. }
  112. };
  113.  
  114. Chatter.prototype.handleChat = function(line) {
  115. this.emit("chat", this, line);
  116. };
  117.  
  118. Chatter.prototype.handleDisconnect = function() {
  119. this.emit("leave", this);
  120. };
  121.  
  122. Chatter.prototype.send = function(data) {
  123. this.socket.write(data + "\r\n");
  124. };
  125.  
  126. /*******************************************************************************
  127. * SocketLineBuffer
  128. *
  129. * Listens for and buffers incoming data on a socket and emits a 'line' event
  130. * whenever a complete line is detected.
  131. ******************************************************************************/
  132.  
  133. function SocketLineBuffer(socket) {
  134. EventEmitter.call(this);
  135.  
  136. this.socket = socket;
  137. this.buffer = "";
  138.  
  139. this.socket.on("data", this.handleData.bind(this));
  140. };
  141.  
  142. sys.inherits(SocketLineBuffer, EventEmitter);
  143.  
  144. SocketLineBuffer.prototype.handleData = function(data) {
  145. for(var i = 0; i < data.length; i++) {
  146. var char = data.charAt(i);
  147. this.buffer += char;
  148. if(char == "\n") {
  149. this.buffer = this.buffer.replace("\r\n", "");
  150. this.buffer = this.buffer.replace("\n", "");
  151. this.emit("line", this.buffer);
  152. this.buffer = "";
  153. }
  154. }
  155. };
  156.  
  157. // Start the server!
  158. server = new ChatServer();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement