Giter VIP home page Giter VIP logo

java-websocket's Introduction

Java WebSockets

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).

Implemented WebSocket protocol versions are:

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 build/examples: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 build/examples:dist/WebSocket.jar ChatClient

Note: If you're on Windows, then replace the : (colon) in the classpath in the commands above with a ; (semicolon).

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.

Minimum Required JDK

Java-WebSocket is known to work with:

  • Java 1.5 (aka SE 6)
  • Android 1.6 (API 4)

Other JRE implementations may work as well, but haven't been tested.

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
}

Getting Support

If you are looking for help using Java-WebSocket you might want to check out the #java-websocket IRC room on the FreeNode IRC network.

License

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

java-websocket's People

Contributors

davidiusdadi avatar tootallnate avatar detro avatar sattailanfear avatar morkai avatar danielywoo avatar jgautier avatar nathanielmichael avatar swax avatar

Stargazers

Enrique-porras15 avatar Connor McKenna avatar

Watchers

James Cloos avatar Connor McKenna avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.