注意:此方式需要进入tomcat根目录导进来一个 java-websocket.1.3.7.jar包哦!
package main;
import java.net.URI;
import org.apache.log4j.Logger;
import org.java_websocket.WebSocket.READYSTATE;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import common.constant.SystemConstant;
import logic.zhuoyue.receive.ReceiveInterval;
import logic.zhuoyue.send.thread.SendHeartThread;
import logic.zhuoyue.send.thread.SendInThread;
import logic.zhuoyue.send.thread.SendMonthCardThread;
import logic.zhuoyue.send.thread.SendOutThread;
/**
* 建立连接...
* 发送数据
* @author apple
*/
public class Client {
private final static Logger logger = Logger.getLogger(Client.class);
public static WebSocketClient client;
public static void createConnect() throws Exception{
//SystemConstant.WS_URL : wsURL地址
client = new WebSocketClient(new URI(SystemConstant.WS_URL), new Draft_6455()) {}
直接把Client客户端实例当做工具类操作即可.
例如: Client.send("发送的信息");
直接在Client类中的onMessage方法中接收,然后在自己处理即可.
http://coolaf.com/tool/chattest
http://www.blue-zero.com/WebSocket/