一、java客户端实现代码
package ;
import ;
import ;
import ;
import org.java_websocket.;
import org.java_websocket.;
public class MsgWebSocketClient extends WebSocketClient{
public MsgWebSocketClient(String url) throws URISyntaxException {
super(new URI(url));
// TODO Auto-generated constructor stub
}
@Override
public void onOpen(ServerHandshake shake) {
// TODO Auto-generated method stub
("握手...");
for(Iterator<String> it=();();) {
String key = ();
(key+":"+(key));
}
}
@Override
public void onMessage(String paramString) {
// TODO Auto-generated method stub
("接收到消息:"+paramString);
}
@Override
public void onClose(int paramInt, String paramString, boolean paramBoolean) {
// TODO Auto-generated method stub
("关闭...");
}
@Override
public void onError(Exception e) {
// TODO Auto-generated method stub
("异常"+e);
}
}
二、单例初始化类:
package ;
import ;
import ;
import ;
import ;
import ;
public enum WebClientEnum {
CLIENT;
private static MsgWebSocketClient socketClient = null;
public static void initClient(MsgWebSocketClient client) {
socketClient = client;
if((socketClient)) {
();
("测试websocket。。。");
}
boolean flag = true;
int i=1000;
while(flag) {
("测试websocket。。。"+(i--));
try {
(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
();
}
if(i == 0) {
flag = false;
}
}
}
}
三、启动主方法:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class SocketClientEngine{
public static void main(String[] args) {
try {
(new MsgWebSocketClient("ws://192.168.152.73:8099"));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
();
}
}
}