A simple WebSocket server running on port 8080 that prints incoming messages to the server console.
- WebSocket server listening on port 8080
- Prints all incoming messages to the console
- Displays connection/disconnection events
- Built with Java-WebSocket library
- Java 21 or higher
- IntelliJ IDEA (recommended)
- Open the project in IntelliJ IDEA
- IntelliJ should automatically download the Java-WebSocket library (org.java-websocket:Java-WebSocket:1.5.6)
- 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
- Open
src/Main.javain IntelliJ IDEA - Run the
main()method - The server will start on
ws://localhost:8080 - You should see: "WebSocket server is running on ws://localhost:8080"
- Open
test-client.htmlin your web browser - Click the "Connect" button
- Type a message and click "Send"
- The message should appear in your server 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!');- Use tools like Postman, wscat, or any WebSocket client
- Connect to:
ws://localhost:8080 - Send messages and watch them appear in the server console
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
Press Ctrl+C in the console or stop the application in IntelliJ IDEA.