SpringBoot集成Socket 长连接

web 能从前端拿到数据 从写好的Socket服务端发送数据 出去 我不知道怎么把这个
从输出流 发送给Client 有大神能帮我看看吗?

下面是开了两个线程 一个来保持TCP连接 一个是 进行输入输出的线程

图片说明图片说明

图片说明

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

浏览器上ctrl+f,然后搜索spring-boot-sample-websocket-tomcat,这个目录里面是spring boot使用socket的简单例子.

@Autowired
private SimpMessagingTemplate template;

//接收客户端"/app/chat"的消息,并发送给所有订阅了"/topic/messages"的用户
@MessageMapping("/chat")
@SendTo("/topic/messages")
public OutputMessage receiveAndSend(InputMessage inputMessage) throws Exception {
    System.out.println("get message (" + inputMessage.getText() + ") from client!");
    System.out.println("send messages to all subscribers!");
    String time = new SimpleDateFormat("HH:mm").format(new Date());
    return new OutputMessage(inputMessage.getFrom(), inputMessage.getText(), time);
}

//或者直接从服务端发送消息给指定客户端
@MessageMapping("/chat_user")
public void sendToSpecifiedUser(@Payload InputMessage inputMessage, SimpMessageHeaderAccessor headerAccessor) throws Exception {
    System.out.println("get message from client (" + inputMessage.getFrom() + ")");
    System.out.println("send messages to the specified subscriber!");
    String time = new SimpleDateFormat("HH:mm").format(new Date());
    this.template.convertAndSend("/topic/" + inputMessage.getFrom(), new OutputMessage(inputMessage.getFrom(), inputMessage.getText(), time));
}作者:syncwt链接:http://www.jianshu.com/p/1e1c36014c80來源:简书著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

浏览器上ctrl+f,然后搜索spring-boot-sample-websocket-tomcat,这个目录里面是spring boot使用socket的简单例子.

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

浏览器上ctrl+f,然后搜索spring-boot-sample-websocket-tomcat,这个目录里面是spring boot使用socket的简单例子.

浏览器上ctrl+f,然后搜索spring-boot-sample-websocket-tomcat,这个目录里面是spring boot使用socket的简单例子.

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

浏览器上ctrl+f,然后搜索spring-boot-sample-websocket-tomcat,这个目录里面是spring boot使用socket的简单例

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples
浏览器上ctrl+f,然后搜索spring-boot-sample-websocket-tomcat,这个目录里面是spring boot使用socket的简单例子.