博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java-WebSocket
阅读量:5904 次
发布时间:2019-06-19

本文共 4901 字,大约阅读时间需要 16 分钟。

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

 

 branch: master 

   

David Rohmer 
 authored on 10 Aug
  3 years ago
  2 years ago
  5 months ago
  7 months ago
  2 years ago
  2 years ago
  3 years ago
  3 years ago
  2 years ago
  2 years ago
  2 years ago
  5 months ago
  5 months ago
  3 years ago
  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  for web browsers).

Implemented WebSocket protocol versions are:

 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: compilejardoc and clean

Maven

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

org.java-websocket
Java-WebSocket
1.3.0

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 .

Writing your own WebSocket Server

The org.java_websocket.server.WebSocketServer abstract class implements the server-side of the. 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 onOpenonClose,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. ( ).

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 .

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 .

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  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.

转载于:https://www.cnblogs.com/fx2008/p/4115188.html

你可能感兴趣的文章
iostat命令学习
查看>>
html video的url更新,自动清缓存
查看>>
【11】ajax请求后台接口数据与返回值处理js写法
查看>>
Python菜鸟之路:Jquery Ajax的使用
查看>>
LeetCode算法题-Maximum Depth of Binary Tree
查看>>
Cox 教学视频5
查看>>
Jenkins持续集成学习-搭建jenkins问题汇总
查看>>
使用ffmpeg实现对h264视频解码 -- (实现了一个易于使用的c++封装库)
查看>>
flink watermark介绍
查看>>
Android Xutils 框架
查看>>
Sysbench 0.5版安装配置
查看>>
书摘—你不可不知的心理策略
查看>>
【博客话题】毕业——开始人生的艰苦历程
查看>>
Linux安装telnet
查看>>
sap scriptfom 多语言翻译
查看>>
黄聪:3分钟学会sessionStorage用法
查看>>
Entity Framework 全面教程详解(转)
查看>>
Windows上Python2.7安装Scrapy过程
查看>>
挖掘数据金矿 领军协同创新 曙光荣膺“2016大数据创新应用领袖企业”称号
查看>>
Fast通道获得Win10 Mobile Build 14977更新
查看>>