Java WebSockets
2016-02-25 12:00
513 查看
https://github.com/TooTallNate/Java-WebSocket
Java WebSockets
This repository contains a barebones WebSocket server and client implementation written in 100% Java. The underlying classes are implementedjava.nio, which allows for a non-blocking event-driven model (similar to the WebSocket API for web browsers).
Implemented WebSocket protocol versions are:
RFC 6455
Hybi 17
Hybi 10
Hixie 76
Hixie 75
Here some more details about protocol versions/drafts.
Build
You can build using Ant or Maven but there is nothing against just putting the source pathsrc/main/javaon your applications buildpath.
Ant
ant
will create the javadoc of this library at
doc/and build the library itself:
dest/java_websocket.jar
The anttargets are:
compile,
jar,
docand
clean
Maven
To use maven just add this dependency to your pom.xml:<dependency> <groupId>org.java-websocket</groupId> <artifactId>Java-WebSocket</artifactId> <version>1.3.0</version> </dependency>
Running the Examples
Note: If you're on Windows, then replace the:(colon) in the classpath in the commands below with a
;(semicolon).
After you build the library you can start the chat server (a
WebSocketServersubclass):
java -cp build/examples:dist/java_websocket.jar ChatServer
Now that the server is started, you need to connect some clients. Run the Java chat client (a
WebSocketClientsubclass):
java -cp build/examples:dist/java_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.
In the example folder is 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
Theorg.java_websocket.server.WebSocketServerabstract 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 yoursubclass to add purpose.
Writing your own WebSocket Client
Theorg.java_websocket.client.WebSocketClientabstract class can connect to valid WebSocket servers. The constructor expects a valid
ws://URI to connect to. Importantevents
onOpen,
onClose,
onMessageand
onIOErrorget fired throughout the life of the WebSocketClient, and must be implemented in your subclass.
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:8000visit the url
https://localhost:8000with 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=allcan 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 multible connections to the same wss server if the server uses a different port than the default port(443).
If you wantto use
wsson 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-WebSocketis 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 usingIPv6 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
IPv6by calling
java.lang.System.setProperty("java.net.preferIPv6Addresses", "false"); java.lang.System.setProperty("java.net.preferIPv4Stack", "true");
somewhere in your project, before instantiating the
WebSocketClientclass. 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 usingJava-WebSocketyou might wantto 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 theLICENSEfile for specifics.
相关文章推荐
- java 启动线程的两种方式
- 10种简单的Java性能优化
- Java连接Elasticsearch集群
- javaAio例子
- Java不同压缩算法的性能比较
- eclipse 汉化教程(中文语言包的下载和安装)
- 使用 Java8 实现观察者模式的方法(下)
- Java修饰符关键词大全
- Java项目导出war包 security alert:integrity check error”
- Java 动态代理
- 10个影响JavaEE性能问题
- 集成 Maven 2 插件到 Eclipse 的过程
- Java编程提高性能时需注意的地方总结
- Java基础:Day11笔记内容 (内部类、权限修饰符 )
- 使用Java8实现观察者模式的方法(上)
- java 基础之工厂
- Java语法糖之foreach
- Java语法糖之foreach
- java中public等权限问题和final的使用
- Java基础:Day10笔记内容 (多态概述、抽象类)