Skip to content

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

License

Notifications You must be signed in to change notification settings

bbogovich/Java-WebSocket

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java WebSockets

Currently supports older drafts 75 and 76 using a Flash Bridge

hybi-07 is supported for the native FireFox implementation in FireFox 6.x

This repository contains a barebones WebSocket server and client implementation written in 100% Java. The underlying classes are implemented using the Java ServerSocketChannel and SocketChannel classes, which allows 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, compile the example classes and JAR file:

ant

Then, start the chat server (a WebSocketServer subclass):

java -cp example:dist/WebSocket.jar ChatServer

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

java -cp example:dist/WebSocket.jar ChatClient

The chat client is a simple Swing GUI application that allows you to send messages to all other connected clients, and receive 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's implementation will be used, otherwise it will fall back to a Flash-based WebSocket Implementation.

Writing your own WebSocket Server

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

Writing your own WebSocket Client

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

Testing in Android Emulator

Please note Android Emulator has issues using IPv6 addresses. Executing any socket related code (like this library) inside it will address an error

java.net.SocketException: Bad address family

You have to manually disable IPv6 by calling

java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
java.lang.System.setProperty("java.net.preferIPv4Stack", "true");

somewhere in your project, before instantiating the WebSocketClient class. You can check if you are currently testing in the Android Emulator like this

if ("google_sdk".equals( Build.PRODUCT )) {
  // ... disable IPv6
}

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

No packages published

Languages

  • Java 70.8%
  • JavaScript 29.1%
  • Shell 0.1%