前言:
如今各位老铁们对“java怎么做客户端”大概比较关怀,各位老铁们都想要了解一些“java怎么做客户端”的相关文章。那么小编在网上网罗了一些对于“java怎么做客户端””的相关内容,希望朋友们能喜欢,各位老铁们快快来了解一下吧!服务端:
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.fedo.forwardplatform.common.Constant;import com.fedo.forwardplatform.common.HashMapDataCache;import com.fedo.forwardplatform.common.ServerDataCache;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.websocket.OnClose;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.PathParam;import javax.websocket.server.ServerEndpoint;import java.util.concurrent.ConcurrentHashMap;@Slf4j@Component@ServerEndpoint("/websocket/{name}")public class WebSocket { private Session session; private String name; private static ConcurrentHashMap<String,WebSocket> webSocketSet = new ConcurrentHashMap<>(); @OnOpen public void OnOpen(Session session,@PathParam(value = "name") String name){ this.session = session; this.name = name; log.info("name:{}",name); // name是用来表示唯一客户端,如果需要指定发送,需要指定发送通过name来区分 webSocketSet.put(str[0],this); log.info("[WebSocket] 连接成功,当前连接数为:={}",webSocketSet.size()); } @OnClose public void OnClose(){ webSocketSet.remove(this.name); serverDataCache.removeCache(this.name); log.info("[WebSocket] 退出成功,当前连接数为:={}",webSocketSet.size()); } @OnMessage public void OnMessage(String message){ log.info("[WebSocket] 待转发消息:{}",message); JSONObject json = JSON.parseObject(message); //https传回消息 //发送对象 String machineName = json.getString(Constant.JSON_MNAME); log.debug("machineName:{}",machineName); if(){ //发送指定socket AppointSending(machineName,message); }else{ GroupSending(message); } } /** * 群发 * @param message */ public void GroupSending(String message){ for (String name : webSocketSet.keySet()){ try { webSocketSet.get(name).session.getBasicRemote().sendText(message); }catch (Exception e){ e.printStackTrace(); } } } /** * 指定发送 * @param name * @param message */ public void AppointSending(String name,String message){ try { webSocketSet.get(name).session.getBasicRemote().sendText(message); }catch (Exception e){ e.printStackTrace(); } }}
java后台客户端:
import java.net.URI;import java.net.URISyntaxException;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.fedo.excuteplatform.utils.HashMapDataCache;import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import org.java_websocket.client.WebSocketClient;import org.java_websocket.drafts.Draft_6455;import org.java_websocket.handshake.ServerHandshake;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Slf4j@Componentpublic class MyWebSocketClient { public WebSocketClient getClient(String uri){ try { //创建客户端连接对象 WebSocketClient client = new WebSocketClient(new URI(uri),new Draft_6455()) { @Override public void onOpen(ServerHandshake handshakedata) { log.info("打开链接"); } @Override public void onMessage(String message) { log.debug("接收消息:{}", message); } @Override public void onClose(int code, String reason, boolean remote) { log.info("关闭链接"); } @Override public void onError(Exception ex) { ex.printStackTrace(); log.error("链接错误:{}", ex.getMessage()); } }; client.connect(); //失败重连 new Thread(new Runnable() { public void run() { System.out.println("Runnable running.."); } }) { @SneakyThrows public void run() { while (true){ Thread.sleep(1000); if(!client.isOpen()){ client.reconnect(); } } } }.start(); return client; }catch (URISyntaxException e){ log.error(e.getMessage()); } return null; }}
问题:在服务端如何单独给websocket一个端口让其与服务本身端口分开?欢迎大家探讨。
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #java怎么做客户端