Compare commits

..

No commits in common. "4565bff3bc873ab53b4766112a3dae172cbd5b11" and "ca3aef3dd358d5186e66ce5d79857a61b85018df" have entirely different histories.

2 changed files with 43 additions and 101 deletions

View File

@ -1,4 +1,3 @@
{
"port": 3004,
"tcp_port": 25555
}
"port": 3004
}

139
index.js
View File

@ -6,12 +6,10 @@
*/
const WebSocket = require("ws");
const net = require("net");
const crypto = require("crypto");
const config = require("./config.json");
const channels = {};
const clients = [];
const WILDCARD = '*';
// ---- FUNCTIONS ----
@ -85,10 +83,8 @@ const server = new WebSocket.Server({ // create the websocket server, port is fr
port: config.port,
});
const netServer = net.createServer();
function getClient(sID) { // just a for loop to get the ws client from the session ID
for (let item of clients) {
for (let item of server.clients) {
if (item.sID === sID) {
return item;
}
@ -97,25 +93,25 @@ function getClient(sID) { // just a for loop to get the ws client from the sessi
function transmit(channel, message, meta, ignore = null) { // transmit a message to the channel. WILDCARD channel is read-only
try {
if (!channels[channel]) return;
if (channel === WILDCARD) { // prevents from sending a message directly to WILDCARD channel
return;
}
if(channels[channel]) {
channels[channel].forEach(sID => {
if (ignore === sID) return;
channels[channel].forEach(sID => {
if (ignore === sID) return;
let ws = getClient(sID);
if (ws) {
ws.send(JSON.stringify({
type: "message",
channel: channel,
message: message,
meta: meta,
}))
}
});
}
let ws = getClient(sID);
if (ws) {
ws.send(JSON.stringify({
type: "message",
channel: channel,
message: message,
meta: meta,
}))
}
});
if (channels[WILDCARD]) { // send message to WILDCARD channel
channels[WILDCARD].forEach(sID => {
@ -135,8 +131,29 @@ function transmit(channel, message, meta, ignore = null) { // transmit a message
}
}
function onMessage(ws) {
return function(message) {
server.on("connection", ws => { // Listen to clients connecting to the websocket server
ws.uuid = random(); // assign a random UUID as guest
ws.sID = random(undefined, "S"); // Session ID
ws.auth = false; // not authenticated by default
console.log("Connect:", ws.uuid, ws.sID);
let pingInterval = setInterval(function () { // Send a ping to the client every 10 seconds to keep the connection alive
ws.send(JSON.stringify({
type: "ping",
uuid: ws.uuid,
ping: Date.now(),
}))
}, 10000);
ws.send(JSON.stringify({ // A friendly message upon connection
type: "motd",
motd: "Welcome to the Soqet network",
uuid: ws.uuid,
}));
ws.on("message", message => {
let data;
try {
data = JSON.parse(message);
@ -172,8 +189,7 @@ function onMessage(ws) {
// create the message meta
let meta = typeof data.meta === "object" && !Array.isArray(data.meta) ? data.meta : {};
let meta = data.meta || {};
meta.uuid = ws.uuid; // sender uuid
meta.time = Date.now(); // time of sending
meta.channel = data.channel; // channel
@ -285,35 +301,7 @@ function onMessage(ws) {
id: data.id,
}))
}
}
}
server.on("connection", ws => { // Listen to clients connecting to the websocket server
ws.uuid = random(); // assign a random UUID as guest
ws.sID = random(undefined, "S"); // Session ID
ws.auth = false; // not authenticated by default
ws.type = "websocket";
clients.push(ws);
console.log("Connect:", ws.uuid, ws.sID);
let pingInterval = setInterval(function () { // Send a ping to the client every 10 seconds to keep the connection alive
ws.send(JSON.stringify({
type: "ping",
uuid: ws.uuid,
ping: Date.now(),
}))
}, 10000);
ws.send(JSON.stringify({ // A friendly message upon connection
type: "motd",
motd: "Welcome to the Soqet network",
uuid: ws.uuid,
}));
ws.on("message", onMessage(ws));
});
ws.on("close", (code, reason) => { // WS Client disconnects
console.log("Close:", ws.uuid, ws.sID, `(${code} ${reason})`);
@ -328,49 +316,4 @@ server.on("connection", ws => { // Listen to clients connecting to the websocket
});
});
netServer.on("connection", socket => {
socket.send = function(data) {
return socket.write(data)
}
socket.uuid = random();
socket.sID = random(undefined, "S");
socket.auth = false;
socket.type = "socket"
clients.push(socket)
console.log("TCP Connect:", socket.uuid, socket.sID);
let pingInterval = setInterval(function () { // Send a ping to the client every 10 seconds to keep the connection alive
socket.send(JSON.stringify({
type: "ping",
uuid: socket.uuid,
ping: Date.now(),
}))
}, 10000);
socket.send(JSON.stringify({ // A friendly message upon connection
type: "motd",
motd: "Welcome to the Soqet network",
uuid: socket.uuid,
}));
socket.on("data", onMessage(socket));
socket.on("close", (code, reason) => { // WS Client disconnects
console.log("Close:", socket.uuid, socket.sID, `(${code} ${reason})`);
clearInterval(pingInterval); // Clear Ping interval
// remove uuid from all channels
disconnect(socket.sID);
});
socket.on("error", (err) => {
console.error(socket.uuid, socket.sID, err) // it can happen
});
})
netServer.listen(config.tcp_port);
// hopefully this service will help cc communities
// hopefully this service will help cc communities