feat: 1.0 stable api
This commit is contained in:
@@ -1,56 +1,66 @@
|
||||
const WebSocket = require('ws');
|
||||
const readline = require('readline');
|
||||
const WebSocket = require("ws");
|
||||
const readline = require("readline");
|
||||
|
||||
const url = 'wss://connect4.abunchofknowitalls.com';
|
||||
console.log(`Connecting to ${url}...`);
|
||||
const ws = new WebSocket(url);
|
||||
const DEFAULT_URL = "wss://connect4.abunchofknowitalls.com";
|
||||
|
||||
let ws;
|
||||
let pingInterval;
|
||||
|
||||
ws.on('open', () => {
|
||||
console.log('Connected to server!');
|
||||
console.log('Type a message and press Enter to send raw text.');
|
||||
function startClient(serverUrl) {
|
||||
console.log(`Connecting to ${serverUrl}...`);
|
||||
ws = new WebSocket(serverUrl);
|
||||
|
||||
ws.on("open", () => {
|
||||
console.log("Connected to server!");
|
||||
console.log("Type a message and press Enter to send raw text.");
|
||||
// Keep the connection alive by sending a ping every 30 seconds
|
||||
pingInterval = setInterval(() => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.ping();
|
||||
}
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.ping();
|
||||
}
|
||||
}, 30000);
|
||||
});
|
||||
});
|
||||
|
||||
ws.on('message', (data) => {
|
||||
ws.on("message", (data) => {
|
||||
// data is usually a Buffer in 'ws', convert to string
|
||||
console.log('< ' + data.toString());
|
||||
});
|
||||
console.log("< " + data.toString());
|
||||
});
|
||||
|
||||
ws.on('pong', () => {
|
||||
ws.on("pong", () => {
|
||||
// console.log('Received pong');
|
||||
});
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('Disconnected from server.');
|
||||
ws.on("close", () => {
|
||||
console.log("Disconnected from server.");
|
||||
if (pingInterval) clearInterval(pingInterval);
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
|
||||
ws.on('error', (err) => {
|
||||
console.error('WebSocket error:', err.message);
|
||||
ws.on("error", (err) => {
|
||||
console.error("WebSocket error:", err.message);
|
||||
if (pingInterval) clearInterval(pingInterval);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
prompt: '> '
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
prompt: "> ",
|
||||
});
|
||||
|
||||
rl.prompt();
|
||||
rl.question(`Enter server address [${DEFAULT_URL}]: `, (answer) => {
|
||||
const serverUrl = answer.trim() || DEFAULT_URL;
|
||||
startClient(serverUrl);
|
||||
|
||||
rl.on('line', (line) => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(line);
|
||||
rl.on("line", (line) => {
|
||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(line);
|
||||
} else {
|
||||
console.log('Socket not open. State:', ws.readyState);
|
||||
const state = ws ? ws.readyState : "N/A";
|
||||
console.log("Socket not open. State:", state);
|
||||
}
|
||||
rl.prompt();
|
||||
});
|
||||
|
||||
rl.prompt();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user