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);
}
}点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦