Chat server: Difference between revisions

Updated JavaScript to newer syntaxes
(Updated JavaScript to newer syntaxes)
Line 1,709:
=={{header|JavaScript}}==
{{works with|Node.js}}
<lang javascript>varconst net = require("net");
varconst sysEventEmitter = require("sysevents").EventEmitter;
var EventEmitter = require("events").EventEmitter;
 
/*******************************************************************************
* ChatServer
Line 1,718 ⟶ 1,717:
* Manages connections, users, and chat messages.
******************************************************************************/
 
functionclass ChatServer() {
constructor() {
this.chatters = {};
this.chatters = {};
this.server = net.createServer(this.handleConnection.bind(this));
this.server = net.listencreateServer(1212, "localhost"this.handleConnection.bind(this));
this.server.listen(1212, "localhost");
}
 
ChatServer.prototype.isNicknameLegal = function(nickname) {
// A nickname may contain letters or numbers only,
// and may only be used once.
if(nickname.replace(/[A-Za-z0-9]*/, '') != "") {
return false
}
for(used_nick in this.chatters) {
if(used_nick == nickname) {
return false;
}
isNicknameLegal(nickname) {
}
// A nickname may contain letters or numbers only,
return true;
// and may only be used once.
};
if (nickname.replace(/[A-Za-z0-9]*/, '') !== "") {
return false;
}
for (const used_nick in this.chatters) {
if (used_nick === nickname) {
return false;
}
}
return true;
}
handleConnection(connection) {
console.log(`Incoming connection from ${connection.remoteAddress}`);
connection.setEncoding("utf8");
 
ChatServer.prototype.handleConnection let chatter = functionnew Chatter(connection), {this);
chatter.on("chat", this.handleChat.bind(this));
console.log("Incoming connection from " + connection.remoteAddress);
chatter.on("join", this.handleJoin.bind(this));
connection.setEncoding("utf8");
chatter.on("leave", this.handleLeave.bind(this));
 
}
var chatter = new Chatter(connection, this);
chatter.on("chat", this. handleChat.bind(thischatter, message)); {
this.sendToEveryChatterExcept(chatter, chatter.nickname + ": " + message);
chatter.on("join", this.handleJoin.bind(this));
}
chatter.on("leave", this.handleLeave.bind(this));
handleJoin(chatter) {
};
console.log(`${chatter.nickname} has joined the chat.`);
 
this.sendToEveryChatter(`${chatter.nickname} has joined the chat.`);
ChatServer.prototype.handleChat = function(chatter, message) {
this.addChatter(chatter);
this.sendToEveryChatterExcept(chatter, chatter.nickname + ": " + message);
};
 
ChatServer.prototype.handleJoin = function(chatter) {
console.log(chatter.nickname + " has joined the chat.");
this.sendToEveryChatter(chatter.nickname + " has joined the chat.");
this.addChatter(chatter);
};
 
ChatServer.prototype.handleLeave = function(chatter) {
console.log(chatter.nickname + " has left the chat.");
this.removeChatter(chatter);
this.sendToEveryChatter(chatter.nickname + " has left the chat.");
};
 
ChatServer.prototype.addChatter = function(chatter) {
this.chatters[chatter.nickname] = chatter;
};
 
ChatServer.prototype.removeChatter = function(chatter) {
delete this.chatters[chatter.nickname];
};
 
ChatServer.prototype.sendToEveryChatter = function(data) {
for(nickname in this.chatters) {
this.chatters[nickname].send(data);
}
};
 
ChatServer.prototype.sendToEveryChatterExcept = function(chatter, data) {
for(nickname in this.chatters) {
if(nickname != chatter.nickname) {
this.chatters[nickname].send(data);
}
handleLeave(chatter) {
}
console.log(`${chatter.nickname} has left the chat.`);
};
this.removeChatter(chatter);
 
this.sendToEveryChatter(`${chatter.nickname} has left the chat.`);
}
addChatter(chatter) {
this.chatters[chatter.nickname] = chatter;
}
removeChatter(chatter) {
delete this.chatters[chatter.nickname];
}
sendToEveryChatter(data) {
for (const nickname in this.chatters) {
this.chatters[nickname].send(data);
}
}
sendToEveryChatterExcept(chatter, data) {
for (const nickname in this.chatters) {
if (nickname !== chatter.nickname) {
this.chatters[nickname].send(data);
}
}
}
}
/*******************************************************************************
* Chatter
Line 1,792 ⟶ 1,785:
* Represents a single user/connection in the chat server.
******************************************************************************/
class Chatter extends EventEmitter {
constructor(socket, server) {
super();
 
function Chatter( this.socket, server)= {socket;
this.server = server;
EventEmitter.call(this);
this.nickname = "";
this.lineBuffer = new SocketLineBuffer(socket);
 
this.lineBuffer.on("line", this.handleNickname.bind(this));
this.socket = socket;
this.socket.on("close", this.handleDisconnect.bind(this));
this.server = server;
this.nickname = "";
this.lineBuffer = new SocketLineBuffer(socket);
 
this.send("Welcome! What is your nickname?");
this.lineBuffer.on("line", this.handleNickname.bind(this));
}
this.socket.on("close", this.handleDisconnect.bind(this));
handleNickname(nickname) {
 
if (server.isNicknameLegal(nickname)) {
this.send("Welcome! What is your nickname?");
this.nickname = nickname;
this.lineBuffer.removeAllListeners("line");
this.lineBuffer.on("line", this.handleChat.bind(this));
this.send(`Welcome to the chat, ${nickname}!`);
this.emit("join", this);
} else {
this.send("Sorry, but that nickname is not legal or is already in use!");
this.send("What is your nickname?");
}
}
handleChat(line) {
this.emit("chat", this, line);
}
handleDisconnect() {
this.emit("leave", this);
}
send(data) {
this.socket.write(data + "\r\n");
}
};
 
sys.inherits(Chatter, EventEmitter);
 
Chatter.prototype.handleNickname = function(nickname) {
if(server.isNicknameLegal(nickname)) {
this.nickname = nickname;
this.lineBuffer.removeAllListeners("line");
this.lineBuffer.on("line", this.handleChat.bind(this));
this.send("Welcome to the chat, " + nickname + "!");
this.emit("join", this);
} else {
this.send("Sorry, but that nickname is not legal or is already in use!");
this.send("What is your nickname?");
}
};
 
Chatter.prototype.handleChat = function(line) {
this.emit("chat", this, line);
};
 
Chatter.prototype.handleDisconnect = function() {
this.emit("leave", this);
};
 
Chatter.prototype.send = function(data) {
this.socket.write(data + "\r\n");
};
 
/*******************************************************************************
* SocketLineBuffer
Line 1,840 ⟶ 1,830:
* whenever a complete line is detected.
******************************************************************************/
class SocketLineBuffer extends EventEmitter {
constructor(socket) {
super();
 
this.socket = socket;
function SocketLineBuffer(socket) {
this.buffer = "";
EventEmitter.call(this);
 
this.socket.on("data", = socketthis.handleData.bind(this));
this.buffer = "";}
handleData(data) {
 
for (let i = 0; i < data.length; i++) {
this.socket.on("data", this.handleData.bind(this));
const char = data.charAt(i);
};
this.buffer += char;
 
if (char == "\n") {
sys.inherits(SocketLineBuffer, EventEmitter);
this.buffer = this.buffer.replace("\r\n", "");
 
this.buffer = this.buffer.replace("\n", "");
SocketLineBuffer.prototype.handleData = function(data) {
this.emit("line", this.buffer);
for(var i = 0; i < data.length; i++) {
var char = data this.charAt(i)buffer = "";
this.buffer += char; }
if(char == "\n") { }
this.buffer = this.buffer.replace("\r\n", "");
this.buffer = this.buffer.replace("\n", "");
this.emit("line", this.buffer);
this.buffer = "";
}
}
};
 
// Start the server!
server = new ChatServer();</lang>
Anonymous user