轻拔琴弦|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
推荐阅读
- 轻拔琴弦|体现着一个人的修养涵养,使用手机
- 轻拔琴弦最惨的不是华为,中兴被美国罚款23亿美元,如今逆袭崛起!
- 上海市徐汇区人民政府门户网站|拨动竖琴的琴弦,画下夕阳下的“馄饨皮”,上海交响乐团的文化集市如此有趣
- 后海大鲨鱼|《乐夏2》被黑最惨的乐队,重回舞台后躁到扔话筒、拆琴弦!
- 轻拔琴弦从差点倒闭到年入10亿,创米李建新:没到最后,我绝不投降
- 轻拔琴弦|构陷华为的汇丰卷入洗钱案件,这回还能洗白?,再次陷入大丑闻
- 轻拔琴弦|Games宣布《堡垒之夜:拯救世界》将不再能在macOS上玩了,Epic
- 轻拔琴弦Epic Games宣布《堡垒之夜:拯救世界》将不再能在macOS上玩了
- 轻拔琴弦 却成为了引领者,“疯子”诺基亚当年发布像素4100万的神机
- 轻拔琴弦我才发现,微信里面有一个功能,能将手机照片自动拼成长图