Skip to content

javaConductor/Java-WebSocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java WebSockets

CAUTION/TODO: This WebSocket implementation adheres to an older draft of the WebSocket specification (draft 75). The current draft (draft 76) is a TODO item/fork-worthy!

This repository contains a simple WebSocket server and client implementation in Java. The underlying classes use the Java classes ServerSocketChannel and SocketChannel, to allow for a non-blocking event-driven model (similar to the WebSocket API for web browsers).

Running the Example

There's a simple chat server and client example located in the example folder.

First, start the chat server (a WebSocketServer subclass): cd example/ java ChatServer

Now that the server is started, we need to connect some clients. Run the Java chat client (a WebSocketClient subclass): java ChatClient

The chat client is a simple Swing GUI that allows you to send messages to all other connected clients, and recieve messages from others in a text box.

There's also a simple HTML file chat client chat.html, which can be opened by any browser. If the browser natively supports the WebSocket API, then it will be used, otherwise it will fall back to a Flash-based WebSocket Implementation.

Writing your own WebSocket Server

A WebSocket server by itself doesn't do anything except establish socket connections though HTTP. After that it's up to a subclass to add purpose. The WebSocketServer class implements the server-side of the WebSocket Protocol.

Writing your own WebSocket Client

The WebSocketClient class can connect to valid WebSocket servers. The constructor expects a valid ws:// URI to connect to. Important events onOpen, onClose, and onMessage get fired throughout the life of the WebSocketClient, and must be implemented in your subclass.

License

Everything found in this repo is licensed under an MIT license. See the LICENSE file for specifics.

About

A barebones WebSocket client and server implementation written in 100% Java.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors