Java-WebSocket README

The project's README from github.com/TooTallNate/Java-WebSocket. As one markdown file: README.md.

Java WebSockets

Javadocs Maven Central

This repository contains a barebones WebSocket server and client implementation written in 100% Java. The underlying classes are implemented java.nio, which allows for a non-blocking event-driven model (similar to the WebSocket API for web browsers).

Implemented WebSocket protocol versions are:

Here some more details about protocol versions/drafts. PerMessageDeflateExample enable the extension with reference to both a server and client example.

Getting Started

Dependency management tools

Below is a brief guide to using dependency management tools like maven or gradle.

Maven

To use maven add this dependency to your pom.xml:

<dependency>
  <groupId>org.java-websocket</groupId>
  <artifactId>Java-WebSocket</artifactId>
  <version>1.6.0</version>
</dependency>

Gradle

To use Gradle add the maven central repository to your repositories list:

mavenCentral()

Then you can just add the latest version to your build.

compile "org.java-websocket:Java-WebSocket:1.6.0"

Or this option if you use gradle 7.0 and above.

implementation 'org.java-websocket:Java-WebSocket:1.6.0'

Logging

This library uses SLF4J for logging and does not ship with any default logging implementation.

Exceptions are using the log level ERROR and debug logging will be done with log level TRACE.

Feel free to use whichever logging framework you desire and use the corresponding binding in your dependency management.

If you want to get started, take a look at the SimpleLogger example.

Standalone jar

If you do not use any dependency management tool, you can find the latest standalone jar here.

Writing your own WebSocket Server

The org.java_websocket.server.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.

An example for a WebSocketServer can be found in both the wiki and the example folder.

Writing your own WebSocket Client

The org.java_websocket.client.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 onError get fired throughout the life of the WebSocketClient, and must be implemented in your subclass.

An example for a WebSocketClient can be found in both the wiki and the example folder.

Examples

You can find a lot of examples here.

WSS Support

This library supports wss. To see how to use wss please take a look at the examples.

If you do not have a valid certificate in place then you will have to create a self signed one. Browsers will simply refuse the connection in case of a bad certificate and will not ask the user to accept it. So the first step will be to make a browser to accept your self signed certificate. ( https://bugzilla.mozilla.org/show_bug.cgi?id=594502 ).
If the websocket server url is wss://localhost:8000 visit the url https://localhost:8000 with your browser. The browser will recognize the handshake and allow you to accept the certificate.

The vm option -Djavax.net.debug=all can help to find out if there is a problem with the certificate.

It is currently not possible to accept ws and wss connections at the same time via the same websocket server instance.

For some reason Firefox does not allow multiple connections to the same wss server if the server uses a different port than the default port (443).

If you want to use wss on the android platform you should take a look at this.

I ( @Davidiusdadi ) would be glad if you would give some feedback whether wss is working fine for you or not.

Minimum Required JDK

Java-WebSocket is known to work with:

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

License

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

Java-WebSocket (TooTallNate/Java-WebSocket)

Searching Database…

这是什么?Java-WebSocket 是一个基础的 WebSocket 客户端和服务端实现,完全用 100% Java 编写,基于 java.nio 构建,采用非阻塞、事件驱动的模型。
这是一个库还是独立应用?它是一个 Java 库:你需要在自己的代码中继承抽象类 WebSocketServer 或 WebSocketClient,从而构建服务端或客户端。
我该如何把它添加到我的项目中?可以将它添加为 Maven 或 Gradle 依赖;如果你不使用依赖管理工具,也可以直接下载独立的 jar 包。
我需要先配置日志框架吗?该库使用 SLF4J 记录日志,但不附带任何默认实现,因此你需要自行选择并绑定日志框架。
使用安全 WebSocket(wss)有什么需要注意的坑吗?同一个服务端实例无法同时接受 ws 和 wss 连接;Firefox 拒绝在非默认端口上对 wss 服务器建立多个连接;自签名证书还需要先在浏览器中手动接受。
它需要哪个 Java 版本?需要 Java 8 或更高版本;其他 JRE 实现可能也能运行,但官方未经测试。
它支持哪些 WebSocket 协议版本?它实现了 RFC 6455 和 RFC 7692(permessage-deflate 扩展)。
它支持压缩吗?支持。它包含一个 PerMessageDeflateExample 示例,展示了如何在服务端和客户端上启用 permessage-deflate 扩展。
可以在 Android 上使用吗?README 提到了对 Android 上 wss 的支持,并附有该平台的额外指南链接,不过它主要还是一个面向桌面端/服务端的 Java 库。
Size
4.1 MB
Stars
10.8K
Age
16y
Commits · 90 Days