From c23bd5567b8762c1994b7abb90135ad1e227e810 Mon Sep 17 00:00:00 2001 From: Devstopfix Date: Thu, 9 Oct 2025 05:45:39 +0100 Subject: [PATCH 1/2] JS Skeleton --- clients/client.js | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 clients/client.js diff --git a/clients/client.js b/clients/client.js new file mode 100644 index 0000000..304f993 --- /dev/null +++ b/clients/client.js @@ -0,0 +1,76 @@ +// Import the WebSocket library +const WebSocket = require('ws'); +// Import the readline module to read keypresses +const readline = require('readline'); + +// --- Configuration --- +// Connect to a WebSocket server on the local machine. +// Replace '127.0.0.1' with your server's IP address if it's on your local network. +const SERVER_ADDRESS = 'ws://122.248.228.103/0/ship/JEV'; + +// --- WebSocket Connection --- +console.log(`Attempting to connect to ${SERVER_ADDRESS}`); +const ws = new WebSocket(SERVER_ADDRESS); + +// --- Event Handlers --- + +// Handle the connection opening +ws.on('open', function open() { + console.log('Successfully connected to the server!'); + console.log('Press "j" for LEFT, "k" for RIGHT, SPACE for FIRE. Press CTRL+C to exit.'); +}); + +// Handle incoming messages from the server +ws.on('message', function incoming(data) { + console.log(`Received from server: ${data}`); +}); + +// Handle connection errors +ws.on('error', function error(err) { + console.error('Connection Error:', err.message); + console.error('Please make sure the WebSocket server is running.'); + process.exit(1); // Exit the program on error +}); + +// Handle the connection closing +ws.on('close', function close() { + console.log('Disconnected from the server.'); + process.exit(0); // Exit the program cleanly +}); + +// --- Keypress Handling --- + +// Set up readline to listen for single keypresses +readline.emitKeypressEvents(process.stdin); +if (process.stdin.isTTY) { + process.stdin.setRawMode(true); +} + +// Listen for the 'keypress' event +process.stdin.on('keypress', (str, key) => { + // Exit the program if CTRL+C is pressed + if (key.ctrl && key.name === 'c') { + ws.close(); + } + + // Check if the connection is open before sending + if (ws.readyState === WebSocket.OPEN) { + let message = ''; + + if (key.name === 'j') { + message = '{}'; + } else if (key.name === 'k') { + message = '{}'; + } else if (key.name === 'space') { + message = '{"fire":true}'; + } + + // If a valid key was pressed, send the message + if (message) { + console.log(`Sending: ${message}`); + ws.send(message); + } + } else { + console.log('WebSocket is not connected. Cannot send message.'); + } +}); From 40e4075b950120f3d3e4fc699ab95543624a7a4d Mon Sep 17 00:00:00 2001 From: Devstopfix Date: Thu, 9 Oct 2025 16:50:49 +0800 Subject: [PATCH 2/2] Configure URL --- clients/client.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/clients/client.js b/clients/client.js index 304f993..49fdf93 100644 --- a/clients/client.js +++ b/clients/client.js @@ -1,71 +1,71 @@ -// Import the WebSocket library +// Import the WebSocket library with `npm install ws` const WebSocket = require('ws'); // Import the readline module to read keypresses const readline = require('readline'); // --- Configuration --- -// Connect to a WebSocket server on the local machine. -// Replace '127.0.0.1' with your server's IP address if it's on your local network. -const SERVER_ADDRESS = 'ws://122.248.228.103/0/ship/JEV'; +// Change your ship name to your initials +const YOUR_NAME_3 = 'YOUXX' +const SERVER = '54.251.134.162' +const SERVER_ADDRESS = 'ws://' + SERVER + '/0/ship/' + YOUR_NAME_3; // --- WebSocket Connection --- -console.log(`Attempting to connect to ${SERVER_ADDRESS}`); +console.log(`Connecting to ${SERVER_ADDRESS}`); const ws = new WebSocket(SERVER_ADDRESS); // --- Event Handlers --- // Handle the connection opening ws.on('open', function open() { - console.log('Successfully connected to the server!'); console.log('Press "j" for LEFT, "k" for RIGHT, SPACE for FIRE. Press CTRL+C to exit.'); }); // Handle incoming messages from the server ws.on('message', function incoming(data) { - console.log(`Received from server: ${data}`); + console.log(`Game state from server: ${data}`); }); // Handle connection errors ws.on('error', function error(err) { console.error('Connection Error:', err.message); - console.error('Please make sure the WebSocket server is running.'); process.exit(1); // Exit the program on error }); // Handle the connection closing ws.on('close', function close() { console.log('Disconnected from the server.'); - process.exit(0); // Exit the program cleanly + process.exit(0); }); -// --- Keypress Handling --- - // Set up readline to listen for single keypresses readline.emitKeypressEvents(process.stdin); if (process.stdin.isTTY) { process.stdin.setRawMode(true); } -// Listen for the 'keypress' event +// Radians +// 0 - EAST +// π/2 - SOUTH +// π - WEST +// 3π/2 - NORTH +var ship_theta = 0.0; + process.stdin.on('keypress', (str, key) => { - // Exit the program if CTRL+C is pressed if (key.ctrl && key.name === 'c') { ws.close(); } - // Check if the connection is open before sending if (ws.readyState === WebSocket.OPEN) { let message = ''; if (key.name === 'j') { - message = '{}'; + message = '{"theta": ' + ship_theta + '}'; } else if (key.name === 'k') { - message = '{}'; + message = '{"theta": ' + ship_theta + '}'; } else if (key.name === 'space') { message = '{"fire":true}'; } - // If a valid key was pressed, send the message if (message) { console.log(`Sending: ${message}`); ws.send(message);