WevSocket(java基于注解实现)

一、概述

本文基于spring-boot-starter-websocket简单的完成收发信息功能,使用注解形式进行实现。

二、相关配置

spring:2.0.2,jdk:1.8.202,maven:3.3.9

因为spring和maven有版本匹配的要求,请大家注意自己的版本是否匹配

三、项目结构

WevSocket(java基于注解实现)_第1张图片

四、代码

1.启动类

SocketDemoApplication
package com.lp.socketdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class SocketDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SocketDemoApplication.class, args);
    }

}

2. 监听类

WsServerEndpoint
package com.lp.socketdemo.java;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
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.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 监听websocket地址(myWs)
 */
@ServerEndpoint("/myWs")
@Component
public class WsServerEndpoint {

    static Map map = new ConcurrentHashMap<>();
    @OnOpen
    public void onOpen(Session session) {
        map.put(session.getId(),session);
        System.out.println("webSockte is open: " + session.getId());
        sendAll(session,"上线");
    }

    @OnMessage
    public String onMessage(String message) {
        System.out.println("收到一条信息:"+message);
        return "已收到你的消息:"+message;
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("webSockte is close:"+session.getId());
        sendAllEceptMe(session,"下线");
        map.remove(session.getId());

    }

    @Scheduled(fixedRate = 60000)
    public void sendMsg() {
        for (String key: map.keySet()) {
            try {
                map.get(key).getBasicRemote().sendText("心跳");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void sendAll(Session session,String msg) {
        for (String key: map.keySet()) {
            try {
                map.get(key).getBasicRemote().sendText(session.getId()+":"+msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void sendAllEceptMe(Session session,String msg) {
        for (String key: map.keySet()) {
            try {
                if(!session.getId().equals(key)) {
                    map.get(key).getBasicRemote().sendText(session.getId()+":"+msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3.配置类

WebSocketConfig
package com.lp.socketdemo.java;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * 配置类,注入bean
 */
@Configuration
public class WebSocketConfig {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

4.前段

websocketTest.html




  
  websocket调试页面



location:


message:

消息记录

五、效果展示

WevSocket(java基于注解实现)_第2张图片

六、引用

课程介绍_WebSocket入门与案例实战-慕课网

最全面的SpringMVC教程(六)——WebSocket_springmvc websocket_小新要变强的博客-CSDN博客

你可能感兴趣的:(websocket,java)