Java-WebSocket
A barebones WebSocket client and server implementation written in 100% Java.
Merge pull request #260 from 6wunderkinder/master …
| autobahn reports | added autobahn server reports | 3 years ago | |
| dist | making use of javac target option to keep binary compatibility to jav… | 2 years ago | |
| gradle/wrapper | Updating gradle wrapper to 1.12 | 5 months ago | |
| src | * Fix stack overflow error | 7 months ago | |
| .gitignore | Ignore idea and gradle outputs | 2 years ago | |
| .travis.yml | Enable travis | 2 years ago | |
| CodeFormatterProfile.xml | removed unnecessary code formatter profile and changed insert_space_b… | 3 years ago | |
| LICENSE | Update year in LICENSE file. | 3 years ago | |
| README.markdown | updated maven section in the readme | 2 years ago | |
| build.gradle | Ignore idea and gradle outputs | 2 years ago | |
| build.xml | making use of javac target option to keep binary compatibility to jav… | 2 years ago | |
| gradlew | Updating gradle wrapper to 1.12 | 5 months ago | |
| gradlew.bat | Updating gradle wrapper to 1.12 | 5 months ago | |
| index.html | Created gh-pages branch via GitHub | 3 years ago | |
| pom.xml | updated maven section in the readme | 2 years ago |
README.markdown
Java WebSockets
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.
Build
You can build using Ant or Maven but there is nothing against just putting the source path src/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 ant targets are: compile, jar, doc and 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 WebSocketServer subclass):
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 (aWebSocketClient subclass):
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
The org.java_websocket.server.WebSocketServer abstract class implements the server-side of theWebSocket 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 org.java_websocket.server.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.
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 multible 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.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的更多相关文章
- 使用Jetty搭建Java Websocket Server,实现图像传输
https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...
- Java WebSocket 线程安全的保证
Java WebSocket线程安全基于3点: 1 在新的客户端连接时,WebSocket容器会创建一个新的端点实例,对应的会话实例表示从唯一的客户端到该端点实例的唯一连接. 2 每个WebSocke ...
- Java WebSocket HttpSession与WebSocket Session的关联
当HttpSession中止(通过显示地失效或超时)时,Web容器会把HttpSession属性从HttpSession中清除. javax.servlet.http.HttpSessionBindi ...
- java websocket学习
引言: websocket,webservice傻傻分不清楚,都觉得是很高深的东西,理解中的webservice是一种协议,通信协议,类似http协议的那种,比如使用webservice协议调后台接口 ...
- java websocket开发的客户端程序
最近用java websocket开发的客户端程序,在和服务端链接通后,在数据传输完毕后,客户端自动关闭了链接,如何能保持链接不断开 这个是客户端的启动类,在循环完毕后,会自动断开和服务器的链接,开始 ...
- java WebSocket Demo
1.IDEA创建Module,结构如图(Tomcat8.0) 2.引入jar包:javax.websocket-api.jar 3.新建WebSocketTest类 import javax.webs ...
- Java WebSocket实现网络聊天室(群聊+私聊)
1.简单说明 在网上看到一份比较nice的基于webSocket网页聊天项目,准备看看学习学习,如是有了这篇文章!原博主博客:http://blog.csdn.net/Amayadream/artic ...
- JAVA WebSocKet ( 简单的聊天室 )
1, 前端代码 登入页 -> login.html <!DOCTYPE html> <html> <head> <meta charset=" ...
- JAVA WebSocKet ( 实现简单的前后端即时通信 )
1, 前端代码 HTML5 部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...
- java websocket @ServerEndpoint注解说明
http://www.blogjava.net/qbna350816/archive/2016/07/24/431302.html https://segmentfault.com/q/1010000 ...
随机推荐
- 10. 将摄像机对准物体,并显示整个对准过程,摄像机Zoom
1. 如果把代码放到按钮事件中调用,达不到想要的效果 2. 可以不用委托,但是要在Update函数中写调用CameraZoonIn的代码 3. 有很多需要改进的地方,可以参考使用 iTween 插件达 ...
- Android java.lang.ClassCastException
lassCastException,从字面上看,是类型转换错误,通常是进行强制类型转换时候出的错误.下面对产生ClassCastException异常的原因进行分析,然后给出这种异常的解决方法. 这种 ...
- lib-flexible 结合 WKWebView 的样式错乱解决方法
技术栈 lib-flexible 是淘宝的可伸缩方案 WKWebView 是ios8以上支持的网页控件 问题场景 最新公司一个项目使用 lib-flexible 来做移动端的伸缩解决方案,页面在saf ...
- Android 自定义title样式
requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性.参数是Window类中定义的常量.一.枚举常量1.DEFAULT_FEATURES:系统默认状态,一般不需 ...
- hadoop的wordcount例子运行
可以通过一个简单的例子来说明MapReduce到底是什么: 我们要统计一个大文件中的各个单词出现的次数.由于文件太大.我们把这个文件切分成如果小文件,然后安排多个人去统计.这个过程就是”Map”.然后 ...
- 转 脸书pop动画的五个步骤
http://blog.csdn.net/u013741809/article/details/38511741 5 Steps For Using Facebook Pop // 1. Pick ...
- 在Sharepoint2010中发布VS2010开发的WebPart
转:http://www.cnblogs.com/bfgl/archive/2012/03/22/2411698.html 本人接触Sharepoint2010到今天为止不到一个月.作为一名老C#程序 ...
- POJ 2249
#include<stdio.h> longlong sum; int main() { int k,n,m; while(~scanf("%d%d",&n,& ...
- SVN记住用户名和密码后如何修改
今天遇到一个SVN检出代码用户验证问题.由于自己最近参与了好几个项目,一时间忙不过来.所以希望跟着自己的试用期的同事帮我测试一下刚修改完成的新功能是否有问题.但是该同事没有项目中权限,正好今天恰逢星期 ...
- SSAS数据挖掘算法简介
决策树分析算法:以二叉树的形式展现,分析出影响某种行为(如购买自行车)的因素,并对这些因素排序. 聚类分析算法:物以类聚,人以群分.分析特定群体所共同含有的属性(因素). 未完,待续..