Created
December 14, 2019 20:14
-
-
Save fador/7e1cda7edbe7aa8baaa281e75792878b to your computer and use it in GitHub Desktop.
90's chat server system in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var url = require('url'); | |
// Client data | |
// connected socket | |
// if this is the chatwindow client -> send messages | |
var Client = function Client() { | |
this.socket = null; | |
this.chatWindow = false; | |
}; | |
var Chat = function Chat() { | |
this.clients = []; | |
this.socket = null; | |
setInterval(function () { | |
//console.log('Connected clients '+global.server.clients.length); | |
//global.server.sendAllClients("Example chat message...</br>\r\n"); | |
}, 1000); | |
}; | |
Chat.prototype.init = function () { | |
var port = 8899; | |
this.socket = net.createServer(this.connection_open.bind(this)); | |
this.socket.listen(port, function() { | |
console.log('Server listening on port '+port); | |
}); | |
}; | |
Chat.prototype.sendAllClients = function (data) { | |
for(var i = 0; i < this.clients.length; i++) | |
{ | |
var client = this.clients[i]; | |
if(client.chatWindow) client.socket.write(data); | |
} | |
}; | |
function escapeHTML(s) { | |
return s.replace(/&/g, '&') | |
.replace(/"/g, '"') | |
.replace(/</g, '<') | |
.replace(/>/g, '>'); | |
} | |
Chat.prototype.connection_open = function (c) { | |
var client = new Client(); | |
this.clients.push(client); | |
client.socket = c; | |
function connClosed() { | |
var index = this.clients.indexOf(client); | |
if (index !== -1) { | |
this.clients.splice(index, 1); | |
console.log("Client disconnected"); | |
} | |
} | |
function readInput(data) { | |
var request = data.toString(); | |
var requestLines = request.split("\r\n"); | |
var mainRequest = requestLines[0].split(" "); | |
if(mainRequest.length != 3) { | |
client.socket.end(); | |
} | |
console.log(mainRequest[1]); | |
// Send frameset if loading the root | |
if(mainRequest[1] == "/") { | |
var htmlData ="<html><frameset rows=\"*,25%\">\n" + | |
" <frame src=\"\chatFrame.html\">\n" + | |
" <frame src=\"\msgFrame.html\">\n" + | |
"</frameset></html>"; | |
var headers = "HTTP/1.1 200 OK\r\n" + | |
"Server: Custom/Chat\r\n" + | |
"Connection: close\r\n" + | |
"Content-Type: text/html\r\n" + | |
"Content-Length: "+htmlData.length+"\r\n" + | |
"\r\n" + htmlData; | |
client.socket.write(headers); | |
client.socket.end(); | |
} | |
// Send the updating chat frame, keep-alive | |
else if(mainRequest[1] == "/chatFrame.html") { | |
var headers = "HTTP/1.1 200 OK\r\n" + | |
"Server: Custom/Chat\r\n" + | |
"Connection: keep-alive\r\n" + | |
"Content-Type: text/html\r\n" + | |
"\r\n" + | |
"<html><head><title>Test Chat</title></head><body>Messages coming<br />"; | |
client.socket.write(headers); | |
client.chatWindow = true; | |
} | |
// Send a frame with nick and message input | |
else if(mainRequest[1].substr(0,14) == "/msgFrame.html") { | |
var nick = "nick"; | |
var input = url.parse(mainRequest[1],true); | |
if(input.query.nick) nick = input.query.nick; | |
var headers = "HTTP/1.1 200 OK\r\n" + | |
"Server: Custom/Chat\r\n" + | |
"Connection: close\r\n" + | |
"Content-Type: text/html\r\n" + | |
"\r\n" + | |
"<html><head><title>Test Chat</title></head><body>"+ | |
"<form action=\"sendMessage.html\" method=\"GET\"><input type=\"text\" name=\"nick\" value=\""+nick+"\"><input type=\"text\" name=\"message\"><input type=\"submit\"></form><br />"; | |
client.socket.write(headers); | |
client.socket.end(); | |
} | |
// Use this to send actual messages, redirect to msgFrame.html | |
else if(mainRequest[1].substr(0,17) == "/sendMessage.html") { | |
var input = url.parse(mainRequest[1],true); | |
var nick = "nick"; | |
if(input.query.nick) nick = input.query.nick; | |
if(input.query.message && input.query.nick) global.server.sendAllClients(escapeHTML("<"+input.query.nick+"> "+input.query.message)+"<br />"); | |
var msgHeaders = "HTTP/1.1 307 Temporary Redirect\r\n" + | |
"Location: /msgFrame.html?nick="+nick+"\r\n" + | |
"\r\n"; | |
client.socket.write(msgHeaders); | |
client.socket.end(); | |
} else { | |
client.socket.end(); | |
} | |
}; | |
c.on("close", connClosed.bind(this)); | |
c.on("error", connClosed.bind(this)); | |
c.on("data", readInput.bind(this)); | |
}; | |
global.server = new Chat(); | |
global.server.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment