美文网首页
webSocket实现

webSocket实现

作者: 账号已被注销 | 来源:发表于2019-07-05 22:48 被阅读0次

    源码地址放在文章末尾


    package Mr.He.spring.controller;
    
    import Mr.He.spring.entity.WebsocketMessage;
    import Mr.He.spring.service.MessageService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    /**
     * WebSocketController
     * @author admin
     */
    @Controller
    @RequestMapping("/websocket/")
    public class WebSocketController {
    
        @Autowired
        private MessageService messageService;
    
        @RequestMapping("index")
        public String index() {
            return "SocketDemo";
        }
    
        @ResponseBody
        @RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
        public void sengMeaasge(@RequestBody WebsocketMessage message) {
            messageService.sendMessage(message.getMessage(), message.getSid());
        }
    }
    
    package Mr.He.spring.service;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import javax.websocket.Session;
    
    @Slf4j
    @Service
    public class MessageService {
    
        @Autowired
        private WebSocketServer webSocketServer;
    
        public void sendMessage(String message, String sid) {
            Session session = webSocketServer.webSocketMap.get(sid);
            if (session == null) {
                return;
            }
            webSocketServer.sendMessage(session, "发往客户端的消息-->" + message);
        }
    }
    
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    import javax.websocket.server.ServerEndpointConfig;
    
    public class CustomSpringConfigurator extends ServerEndpointConfig.Configurator implements ApplicationContextAware {
    
        private static volatile BeanFactory context;
    
        @Override
        public <T> T getEndpointInstance(Class<T> clazz) {
            return context.getBean(clazz);
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            CustomSpringConfigurator.context = applicationContext;
        }
    }
    
    package Mr.He.spring.config;
    
    import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.socket.server.standard.ServerEndpointExporter;
    
    @ConditionalOnWebApplication
    @Configuration
    public class WebSocketConfig {
    
        @Bean
        public ServerEndpointExporter serverEndpointExporter() {
            return new ServerEndpointExporter();
        }
    
        @Bean
        public CustomSpringConfigurator customSpringConfigurator() {
            return new CustomSpringConfigurator();
        }
    }
    

    /**
    *  源码地址github
    */
    https://github.com/HeQuanX/learning
    

    相关文章

      网友评论

          本文标题:webSocket实现

          本文链接:https://www.haomeiwen.com/subject/kdnyhctx.html