Skip to content

Pepijn-DB/TestWebsocketServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSocket Server

A simple WebSocket server running on port 8080 that prints incoming messages to the server console.

Features

  • WebSocket server listening on port 8080
  • Prints all incoming messages to the console
  • Displays connection/disconnection events
  • Built with Java-WebSocket library

Requirements

  • Java 21 or higher
  • IntelliJ IDEA (recommended)

Setup

  1. Open the project in IntelliJ IDEA
  2. IntelliJ should automatically download the Java-WebSocket library (org.java-websocket:Java-WebSocket:1.5.6)
  3. If the library is not downloaded automatically, you can:
    • Right-click on the project → Open Module Settings → Libraries
    • Add the library from Maven: org.java-websocket:Java-WebSocket:1.5.6

Running the Server

  1. Open src/Main.java in IntelliJ IDEA
  2. Run the main() method
  3. The server will start on ws://localhost:8080
  4. You should see: "WebSocket server is running on ws://localhost:8080"

Testing the Server

Option 1: Use the provided HTML test client

  1. Open test-client.html in your web browser
  2. Click the "Connect" button
  3. Type a message and click "Send"
  4. The message should appear in your server console

Option 2: Use browser console

Open your browser's developer console and run:

const ws = new WebSocket('ws://localhost:8080');
ws.onopen = () => console.log('Connected');
ws.onmessage = (event) => console.log('Received:', event.data);
ws.send('Hello from browser!');

Option 3: Use a WebSocket client tool

  • Use tools like Postman, wscat, or any WebSocket client
  • Connect to: ws://localhost:8080
  • Send messages and watch them appear in the server console

Server Output

When messages are received, the server console will display:

Message received: Your message here

The server also logs:

  • New connections: New connection from: /IP:PORT
  • Disconnections: Connection closed: /IP:PORT - Reason: ...
  • Errors: Full stack trace

Stopping the Server

Press Ctrl+C in the console or stop the application in IntelliJ IDEA.

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published