背景
項(xiàng)目分為三個(gè)端,項(xiàng)目之間需要webSocket通信。文章來源地址http://www.zghlxwxcb.cn/news/detail-580146.html
代碼
服務(wù)端
WebSocketConfig
package com.business.socket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
/**
* 如果使用Springboot默認(rèn)內(nèi)置的tomcat容器,則必須注入ServerEndpoint的bean;
* 如果使用外置的web容器,則不需要提供ServerEndpointExporter,下面的注入可以注解掉
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
WebSocketServer
package com.business.socket;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.user.vo.ws.SocMsgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java
文章來源:http://www.zghlxwxcb.cn/news/detail-580146.html
到了這里,關(guān)于java webSocket服務(wù)端、客戶端、心跳檢測優(yōu)雅解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!