轻拔琴弦|java-websocket示例项目,可以直接使用( 五 )
package com.test.test2;/** * Simple example to reconnect blocking and non-blocking. */public class ReconnectClientExample {public static void main(String[] args) throws URISyntaxException, InterruptedException {ExampleClient c = new ExampleClient(new URI("ws://localhost:8887"));//Connect to a server normallyc.connectBlocking();c.send("hi");Thread.sleep(10);c.closeBlocking();//Disconnect manually and reconnect blockingc.reconnectBlocking();c.send("it's");Thread.sleep(10000);c.closeBlocking();//Disconnect manually and reconnectc.reconnect();Thread.sleep(100);c.send("me");Thread.sleep(100);c.closeBlocking();}}
这个例子演示了如何为您的连接使用特定的sec - websocket协议 。package com.test.test2;/** * This example demonstrates how to use a specific Sec-WebSocket-Protocol for your connection. */public class SecWebSocketProtocolClientExample {public static void main(String[] args) throws URISyntaxException {// This draft only allows you to use the specific Sec-WebSocket-Protocol without a fallback.Draft_6455 draft_ocppOnly = new Draft_6455(Collections.
服务器在响应客户端时 , 附带额外的数据package com.test.test2;/** * This example shows how to add additional headers to your server handshake response ** For this you have to override onWebsocketHandshakeReceivedAsServer in your WebSocketServer class ** We are simple adding the additional header "Access-Control-Allow-Origin" to our server response */public class ServerAdditionalHeaderExample extends ChatServer {public ServerAdditionalHeaderExample(int port) {super(new InetSocketAddress(port));}@Overridepublic ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket conn, Draft draft,ClientHandshake request) throws InvalidDataException {ServerHandshakeBuilder builder = super.onWebsocketHandshakeReceivedAsServer(conn, draft, request);builder.put("Access-Control-Allow-Origin", "*");return builder;}public static void main(String[] args) throws InterruptedException, IOException {int port = 8887; // 843 flash policy porttry {port = Integer.parseInt(args[0]);} catch (Exception ex) {}ServerAdditionalHeaderExample s = new ServerAdditionalHeaderExample(port);s.start();System.out.println("Server started on port: " + s.getPort());BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));while (true) {String in = sysin.readLine();s.broadcast(in);if (in.equals("exit")) {s.stop(1000);break;}}}}
推荐阅读
- 轻拔琴弦|体现着一个人的修养涵养,使用手机
- 轻拔琴弦最惨的不是华为,中兴被美国罚款23亿美元,如今逆袭崛起!
- 上海市徐汇区人民政府门户网站|拨动竖琴的琴弦,画下夕阳下的“馄饨皮”,上海交响乐团的文化集市如此有趣
- 后海大鲨鱼|《乐夏2》被黑最惨的乐队,重回舞台后躁到扔话筒、拆琴弦!
- 轻拔琴弦从差点倒闭到年入10亿,创米李建新:没到最后,我绝不投降
- 轻拔琴弦|构陷华为的汇丰卷入洗钱案件,这回还能洗白?,再次陷入大丑闻
- 轻拔琴弦|Games宣布《堡垒之夜:拯救世界》将不再能在macOS上玩了,Epic
- 轻拔琴弦Epic Games宣布《堡垒之夜:拯救世界》将不再能在macOS上玩了
- 轻拔琴弦 却成为了引领者,“疯子”诺基亚当年发布像素4100万的神机
- 轻拔琴弦我才发现,微信里面有一个功能,能将手机照片自动拼成长图