轻拔琴弦|java-websocket示例项目,可以直接使用( 三 )

在访问websocket时携带指定的参数有cookie【轻拔琴弦|java-websocket示例项目,可以直接使用】package com.test.test2;/** * This class shows how to add additional http header like "Origin" or "Cookie". ** To see it working, start ServerRejectHandshakeExample and then start this example. */public class CustomHeaderClientExample {public static void main(String[] args) throws URISyntaxException, InterruptedException {Map httpHeaders = new HashMap();httpHeaders.put("Cookie", "test");ExampleClient c = new ExampleClient(new URI("ws://localhost:8887"), httpHeaders);//We expect no successful connectionc.connectBlocking();httpHeaders.put("Cookie", "username=nemo");c = new ExampleClient(new URI("ws://localhost:8887"), httpHeaders);//Wer expect a successful connectionc.connectBlocking();c.closeBlocking();httpHeaders.put("Access-Control-Allow-Origin", "*");c = new ExampleClient(new URI("ws://localhost:8887"), httpHeaders);//We expect no successful connectionc.connectBlocking();c.closeBlocking();httpHeaders.clear();httpHeaders.put("Origin", "localhost:8887");httpHeaders.put("Cookie", "username=nemo");c = new ExampleClient(new URI("ws://localhost:8887"), httpHeaders);//We expect a successful connectionc.connectBlocking();c.closeBlocking();httpHeaders.clear();httpHeaders.put("Origin", "localhost");httpHeaders.put("cookie", "username=nemo");c = new ExampleClient(new URI("ws://localhost:8887"), httpHeaders);//We expect no successful connectionc.connectBlocking();}}创建websocket的客户端package com.test.test2;/** * This example demonstrates how to create a websocket connection to a server. Only the most * important callbacks are overloaded. */public class ExampleClient extends WebSocketClient {public ExampleClient(URI serverUri, Draft draft) {super(serverUri, draft);}public ExampleClient(URI serverURI) {super(serverURI);}public ExampleClient(URI serverUri, Map httpHeaders) {super(serverUri, httpHeaders);}@Overridepublic void onOpen(ServerHandshake handshakedata) {send("Hello, it is me. Mario :)");System.out.println("opened connection");// if you plan to refuse connection based on ip or httpfields overload: onWebsocketHandshakeReceivedAsClient}@Overridepublic void onMessage(String message) {System.out.println("received: " + message);}@Overridepublic void onClose(int code, String reason, boolean remote) {// The codecodes are documented in class org.java_websocket.framing.CloseFrameSystem.out.println("Connection closed by " + (remote ? "remote peer" : "us") + " Code: " + code + " Reason: "+ reason);}@Overridepublic void onError(Exception ex) {ex.printStackTrace();// if the error is fatal then onClose will be called additionally}public static void main(String[] args) throws URISyntaxException {ExampleClient c = new ExampleClient(new URI("ws://localhost:8887")); // more about drafts here:c.connect();}}这个示例展示了如何发送片段帧package com.test.test2;/** ** This example shows how to send fragmented frames. For information on when to used fragmented * frames see #section-5.4 Fragmented and normal messages can * not be mixed. One is however allowed to mix them with control messages like ping/pong. * * @see WebSocket#sendFragmentedFrame(Opcode, ByteBuffer, boolean) **/public class FragmentedFramesExample {public static void main(String[] args)throws URISyntaxException, IOException, InterruptedException {//WebSocketImpl.DEBUG = true; // will give extra output//连接socket接口WebSocketClient websocket = new ExampleClient(new URI("ws://localhost:8887"));//判断是不是连接成功if (!websocket.connectBlocking()) {System.err.println("Could not connect to the server.");return;}//连接成功 , 进入功能测试阶段System.out.println("This example shows how to send fragmented(continuous) messages.");//读取键盘输入BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));//循环判断 , websocket是不是连接while (websocket.isOpen()) {System.out.println("Please type in a loooooong line(which then will be send in 2 byte fragments):");String longline = stdin.readLine();//将字符串转为字节数组 , 并保存到缓存区ByteBuffer longelinebuffer = ByteBuffer.wrap(longline.getBytes());//倒带缓冲longelinebuffer.rewind();for (int position = 2; ; position += 2) {//判断当前需要读取的下标是不是比缓冲区容量下标小if (position


推荐阅读