Java-WebSocket

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

  • 所有者: TooTallNate/Java-WebSocket
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Java WebSockets

Build Status
Javadocs
Maven Central
Sonatype Nexus (Snapshots)

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.

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.4.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.4.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. This technique is also demonstrated in this video.

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 platfrom 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:

  • Java 1.6 and higher
  • Android 4.0 and higher

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.

主要指标

概览
名称与所有者TooTallNate/Java-WebSocket
主编程语言Java
编程语言Java (语言数: 3)
平台
许可证MIT License
所有者活动
创建于2010-01-20 23:33:04
推送于2025-04-22 14:50:33
最后一次提交2025-04-19 17:21:53
发布数33
最新版本名称v1.6.0 (发布于 )
第一版名称v0.1 (发布于 )
用户参与
星数10.7k
关注者数379
派生数2.6k
提交数1.2k
已启用问题?
问题数1074
打开的问题数44
拉请求数309
打开的拉请求数11
关闭的拉请求数89
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?