java | 使用java-websocket进行websocket客户端通信
java | 使用java-websocket进行websocket客户端通信
主要参考:https://www.cnblogs.com/hhhshct/p/9507446.html
使用到了java-websocket这个库,我没有用org的版本,用的是阿里云maven仓库里的版本,主要代码如下:
package mz.nochat.core;
import com.pusher.java_websocket.client.WebSocketClient;
import com.pusher.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
// 这个类用于和nochat服务器进行连接和交互
public class MzWebSocketClient extends WebSocketClient {
public MzWebSocketClient(String url) throws URISyntaxException{
super(new URI(url));
}
@Override
public void onOpen(ServerHandshake serverHandshake) {
System.out.println("onOpen: 连接握手");
for(Iterator<String> it = serverHandshake.iterateHttpFields(); it.hasNext();) {
String key = it.next();
System.out.println(key+":"+serverHandshake.getFieldValue(key));
}
}
@Override
public void onMessage(String s) {
System.out.println("接收到消息:"+s);
}
@Override
public void onMessage(java.nio.ByteBuffer bytes){
System.out.println("收到字节流消息...");
}
@Override
public void onClose(int i, String s, boolean b) {
System.out.println("连接关闭...");
// 尝试重连
//...
}
@Override
public void onError(Exception e) {
System.out.println("异常"+e);
}
}
使用方式如下:
public static void test_websocket(){
try{
MzWebSocketClient client = new MzWebSocketClient("ws://127.0.0.1:2333");
client.connect();
while (!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
System.out.println("等待连接...");
}
System.out.println("连接成功!");
System.out.println("发送登录包...");
client.send("{\"cmd\":1, \"data\":{\"uname\":\"mz1\",\"passwd\":\"123456\"}}");
}catch (URISyntaxException e){
e.printStackTrace();
}
}
整个客户端的连接会在一个新的线程里面运行,所以主线程只要负责接收啥的应该就可以了。
当然,有时候为了能够使用全局变量啥的,可以直接写匿名内部类:
package mz.nochat.core;
import com.pusher.java_websocket.WebSocket;
import com.pusher.java_websocket.client.WebSocketClient;
import com.pusher.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
public class NoChatClient {
public WebSocketClient client;
// 初始化这个类的时候就会直接连接上
public NoChatClient(String url){
try {
client = new WebSocketClient(new URI(url)) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
System.out.println("onOpen: 连接握手");
for(Iterator<String> it = serverHandshake.iterateHttpFields(); it.hasNext();) {
String key = it.next();
System.out.println(key+":"+serverHandshake.getFieldValue(key));
}
}
@Override
public void onMessage(String s) {
System.out.println("接收到消息:"+s);
}
@Override
public void onClose(int i, String s, boolean b) {
System.out.println("连接关闭...");
// 尝试重连
//...
}
@Override
public void onError(Exception e) {
System.out.println("异常"+e);
}
};
client.connect();
// 直到连接成功再返回
while (!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)){
System.out.println("等待连接...");
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
// 发送登录包
public void login(String uname, String passwd){
}
public static void main(String[] args) {
NoChatClient c = new NoChatClient("ws://127.0.0.1:2333");
}
}
本文来自博客园,作者:Mz1,转载请注明原文链接:https://www.cnblogs.com/Mz1-rc/p/16512480.html
如果有问题可以在下方评论或者email:mzi_mzi@163.com