Untitled
3 years ago in JavaScript
import * as Net from 'net'
import * as ReadLine from 'readline'
import { Server as wServer, Socket as wSocket} from "socket.io";
const readline = ReadLine.createInterface({
input: process.stdin,
output: process.stdout
});
let sockets : Net.Socket[] = [];
let clients : string[] = [];
const Server = new Net.Server();
const io = new wServer();
const PORT = Number(process.env.PORT) || 81;
io.listen(PORT,{
cors: {
origin: "*",
methods: ["GET", "POST"],
allowedHeaders: ["Access-Control-Allow-Origin"]
}
});
Server.listen(80);
Server.on("connection",(socket) =>{
console.log(socket.remoteAddress);
socket.write("Hello to ZAFchat\n");
socket.write("username> ");
let username : string;
socket.on('data',(data)=>{
if (username){
writeAll(username + ": " + data.toString().trimEnd(),socket);
io.sockets.emit("msg",username + ": " + data.toString().trimEnd());
socket.write("> ")
}else{
username = Login(socket,data.toString().trimEnd());
}
});
socket.on('error', error =>{
console.log("me regala un bocadisho?");
});