轻拔琴弦|java-websocket示例项目,可以直接使用( 二 )
演示为websocket赋值id , 这样在收到消息的情况下 , 方便获取到消息的来源package com.test.test2;/** * A simple WebSocketServer implementation. Keeps track of a "chatroom". ** Shows how to use the attachment for a WebSocket. This example just uses a simple integer as ID. * Setting an attachment also works in the WebSocketClient */public class ChatServerAttachmentExample extends WebSocketServer {Integer index = 0;public ChatServerAttachmentExample(int port) throws UnknownHostException {super(new InetSocketAddress(port));}public ChatServerAttachmentExample(InetSocketAddress address) {super(address);}@Overridepublic void onOpen(WebSocket conn, ClientHandshake handshake) {conn.setAttachment(index); //Set the attachment to the current indexindex++;// Get the attachment of this connection as IntegerSystem.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room! ID: "+ conn.
设置访问websocket的设备的需要携带的参数package com.test.test2;/** * This example shows how to reject a handshake as a server from a client. ** For this you have to override onWebsocketHandshakeReceivedAsServer in your WebSocketServer class */public class ServerRejectHandshakeExample extends ChatServer {public ServerRejectHandshakeExample(int port) {super(new InetSocketAddress(port));}@Overridepublic ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket conn, Draft draft,ClientHandshake request) throws InvalidDataException {ServerHandshakeBuilder builder = super.onWebsocketHandshakeReceivedAsServer(conn, draft, request);//In this example we don't allow any resource descriptor ( "ws://localhost:8887/?roomid=1 will be rejected but ws://localhost:8887 is fine)if (!request.getResourceDescriptor().equals("/")) {throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, "Not accepted!");}//If there are no cookies set reject it as well.if (!request.hasFieldValue("Cookie")) {throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, "Not accepted!");}//If the cookie does not contain a specific valueif (!request.getFieldValue("Cookie").equals("username=nemo")) {throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, "Not accepted!");}//If there is a Origin Field, it has to be localhost:8887if (request.hasFieldValue("Origin")) {if (!request.getFieldValue("Origin").equals("localhost:8887")) {throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, "Not accepted!");}}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) {}ServerRejectHandshakeExample s = new ServerRejectHandshakeExample(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万的神机
- 轻拔琴弦我才发现,微信里面有一个功能,能将手机照片自动拼成长图