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 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/java on 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: compilejardoc 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 (a WebSocketClient 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 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

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 onOpenonCloseonMessage 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 WebSockets的更多相关文章

  1. 用Jetty 9.1运行Java WebSockets微服务

    Jetty 9.1的发布将Java WebSockets (JSR-356) 带入了非Java EE环境,从而开启了微服务时代.我们可以将Jetty的容器包含在java应用程序中(注意,不是Java代 ...

  2. Java-WebSocket

    A barebones WebSocket client and server implementation written in 100% Java. http://java-websocket.o ...

  3. Uber License for Android

    Uber license for android list: 1.Butter Knife: 项目地址:https://github.com/JakeWharton/butterknife 这个开源库 ...

  4. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  5. Java Netty 4.x 用户指南

    问题 今天,我们使用通用的应用程序或者类库来实现互相通讯,比如,我们经常使用一个 HTTP 客户端库来从 web 服务器上获取信息,或者通过 web 服务来执行一个远程的调用. 然而,有时候一个通用的 ...

  6. java WebSocket Demo

    1.IDEA创建Module,结构如图(Tomcat8.0) 2.引入jar包:javax.websocket-api.jar 3.新建WebSocketTest类 import javax.webs ...

  7. JAVA JDK1.5-1.9新特性

    1.51.自动装箱与拆箱:2.枚举(常用来设计单例模式)3.静态导入4.可变参数5.内省 1.61.Web服务元数据2.脚本语言支持3.JTable的排序和过滤4.更简单,更强大的JAX-WS5.轻量 ...

  8. 基于Java的WebSocket推送

    WebSocket的主动推送 关于消息推送,现在的解决方案如轮询.长连接或者短连接,当然还有其他的一些技术框架,有的是客户端直接去服务端拿数据. 其实推送推送主要讲的是一个推的概念,WebSocket ...

  9. MQTT, XMPP, WebSockets还是AMQP?泛谈实时通信协议选型 good

    Wolfram Hempel 是 deepstreamIO 的联合创始人.deepstreamIO 是一家位于德国的技术创业公司,为移动客户端.及物联网设备提供高性能.安全和可扩展的实时通信服务.文本 ...

随机推荐

  1. dispatch_group_async 使用详解

    dispatch_group_async可以实现监听一组任务是否完成,完成后得到通知执行其他的操作.这个方法很有用,比如你执行三个下载任务,当三个任务都下载完成后你才通知界面说完成的了.下面是一段例子 ...

  2. Oracle Applications Documentation

    Oracle E-Business Suite Documentation Web Library Release 12.2+ Link Download Oracle E-Business Suit ...

  3. cocos2d-x 输出debug信息

    cocos2d-x 输出debug信息   在Classes目录下添加文件AppDef.h #ifndef _APP_DEF_H_#define _APP_DEF_H_ #include <an ...

  4. Python 标准库一览(Python进阶学习)

    转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...

  5. 7z压缩与解压命令

    在写很多工具的时候,可能会用到7z命令来进行压缩与解压操作.这里记录二个比较常用的操作:压缩.解压.   在dos窗口下输入7z命令,会显示7z的使用参数详情: 7-Zip 9.10 beta  Co ...

  6. CTO、CIO

    对于不从事技术研发的企业,完全不必要设立CTO这一职位,但是CIO是要始终存在为企业提供更好的咨询服务.有时CT0和CIO是同一个人,毕竟是信息时代嘛!CIO的角色从过去IT时代的交付型,转变为DT时 ...

  7. 管道命令和xargs的区别

    一直弄不懂,管道不就是把前一个命令的结果作为参数给下一个命令吗,那在 | 后面加不加xargs有什么区别NewUserFF 写道:懒蜗牛Gentoo 写道:管道是实现“将前面的标准输出作为后面的标准输 ...

  8. SSI(Server Side Include)简单介绍

    Server-side include(server端包含) Server-side include(server端包含)是浏览器向server请求您的文档时并入您的文档的一个文件. 当訪问者浏览器请 ...

  9. 数据库实例: STOREBOOK > 数据文件/退回字段/重做日志组|管理员

    ylbtech-Oracle:数据库实例: STOREBOOK  > 数据文件/退回字段/重做日志组|管理员 数据文件/退回字段/重做日志组|管理员 1. 数据库实例: STOREBOOK  & ...

  10. EF6源码学习-准备篇

    现在对于.net开发人员来说EF已经很流行了,虽然我在2010年的时候就用过EF,也看过几本书,但是还没有仔细研究EF的code, 曾经也尝试阅读EF5的源代码,后来由于时间关系也没有坚持住.现在计划 ...