mirror of https://github.com/Ale32bit/Soqet.git
Compare commits
No commits in common. "4565bff3bc873ab53b4766112a3dae172cbd5b11" and "ca3aef3dd358d5186e66ce5d79857a61b85018df" have entirely different histories.
4565bff3bc
...
ca3aef3dd3
|
@ -1,4 +1,3 @@
|
||||||
{
|
{
|
||||||
"port": 3004,
|
"port": 3004
|
||||||
"tcp_port": 25555
|
|
||||||
}
|
}
|
137
index.js
137
index.js
|
@ -6,12 +6,10 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const WebSocket = require("ws");
|
const WebSocket = require("ws");
|
||||||
const net = require("net");
|
|
||||||
const crypto = require("crypto");
|
const crypto = require("crypto");
|
||||||
const config = require("./config.json");
|
const config = require("./config.json");
|
||||||
|
|
||||||
const channels = {};
|
const channels = {};
|
||||||
const clients = [];
|
|
||||||
const WILDCARD = '*';
|
const WILDCARD = '*';
|
||||||
|
|
||||||
// ---- FUNCTIONS ----
|
// ---- FUNCTIONS ----
|
||||||
|
@ -85,10 +83,8 @@ const server = new WebSocket.Server({ // create the websocket server, port is fr
|
||||||
port: config.port,
|
port: config.port,
|
||||||
});
|
});
|
||||||
|
|
||||||
const netServer = net.createServer();
|
|
||||||
|
|
||||||
function getClient(sID) { // just a for loop to get the ws client from the session ID
|
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) {
|
if (item.sID === sID) {
|
||||||
return item;
|
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
|
function transmit(channel, message, meta, ignore = null) { // transmit a message to the channel. WILDCARD channel is read-only
|
||||||
try {
|
try {
|
||||||
|
if (!channels[channel]) return;
|
||||||
|
|
||||||
if (channel === WILDCARD) { // prevents from sending a message directly to WILDCARD channel
|
if (channel === WILDCARD) { // prevents from sending a message directly to WILDCARD channel
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(channels[channel]) {
|
channels[channel].forEach(sID => {
|
||||||
channels[channel].forEach(sID => {
|
if (ignore === sID) return;
|
||||||
if (ignore === sID) return;
|
|
||||||
|
|
||||||
let ws = getClient(sID);
|
let ws = getClient(sID);
|
||||||
if (ws) {
|
if (ws) {
|
||||||
ws.send(JSON.stringify({
|
ws.send(JSON.stringify({
|
||||||
type: "message",
|
type: "message",
|
||||||
channel: channel,
|
channel: channel,
|
||||||
message: message,
|
message: message,
|
||||||
meta: meta,
|
meta: meta,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (channels[WILDCARD]) { // send message to WILDCARD channel
|
if (channels[WILDCARD]) { // send message to WILDCARD channel
|
||||||
channels[WILDCARD].forEach(sID => {
|
channels[WILDCARD].forEach(sID => {
|
||||||
|
@ -135,8 +131,29 @@ function transmit(channel, message, meta, ignore = null) { // transmit a message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onMessage(ws) {
|
server.on("connection", ws => { // Listen to clients connecting to the websocket server
|
||||||
return function(message) {
|
|
||||||
|
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;
|
let data;
|
||||||
try {
|
try {
|
||||||
data = JSON.parse(message);
|
data = JSON.parse(message);
|
||||||
|
@ -172,8 +189,7 @@ function onMessage(ws) {
|
||||||
|
|
||||||
// create the message meta
|
// 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.uuid = ws.uuid; // sender uuid
|
||||||
meta.time = Date.now(); // time of sending
|
meta.time = Date.now(); // time of sending
|
||||||
meta.channel = data.channel; // channel
|
meta.channel = data.channel; // channel
|
||||||
|
@ -285,35 +301,7 @@ function onMessage(ws) {
|
||||||
id: data.id,
|
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
|
ws.on("close", (code, reason) => { // WS Client disconnects
|
||||||
console.log("Close:", ws.uuid, ws.sID, `(${code} ${reason})`);
|
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
|
Loading…
Reference in New Issue