package com; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.nio.ByteBuffer; import java.nio.channels.NotYetConnectedException; import org.java_websocket.WebSocket.READYSTATE; import org.java_websocket.client.WebSocketClient; import org.java_websocket.drafts.Draft_17; import org.java_websocket.handshake.ServerHandshake; public class Client { public static WebSocketClient client; public static void main(String[] args) throws URISyntaxException, NotYetConnectedException, UnsupportedEncodingException { client = new WebSocketClient(new URI("ws://192.168.126.131:9999"),new Draft_17()) { EstablishXMLFile exf = new EstablishXMLFile(); @Override public void onOpen(ServerHandshake arg0) { System.out.println("打开链接"); } @Override public void onMessage(String arg0) { exf.recieveMessage(arg0); } @Override public void onError(Exception arg0) { arg0.printStackTrace(); System.out.println("发生错误已关闭"); } @Override public void onClose(int arg0, String arg1, boolean arg2) { System.out.println("链接已关闭"); } @Override public void onMessage(ByteBuffer bytes) { try { System.out.println(new String(bytes.array(),"utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }; client.connect(); while(!client.getReadyState().equals(READYSTATE.OPEN)){ System.out.println("还没有打开"); } System.out.println("打开了"); send("hello world".getBytes("utf-8")); client.send("hello world"); } public static void send(byte[] bytes){ client.send(bytes); } }
點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦