Spring boot WebSocket 注入失败

1.在@ServerEndpoint()下面添加

    //此处是解决无法注入的关键
    private static ApplicationContext applicationContext;
    //这里是service
    private WebSocketService webSocketService;

2.继续添加

public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

3.使用方法

webSocketService=applicationContext.getBean(WebSocketService.class);
        webSocketService.setUserStatus(1L,"online");

4.完整代码

package com.xbjs.webim.controller;


import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


import com.xbjs.webim.service.WebSocketService;
import com.xbjs.webim.service.serviceimpl.WebSocketServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.SpringConfigurator;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;


/**
 * Created with IntelliJ IDEA.
 *
 * @author: xincheng.zhao
 * @date: 2018/9/4
 * @description:
 */

@ServerEndpoint(value = "/websocket/{userId}")
@Component
public class WSController {

    //concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象。
    private static Map webSocketmap = new ConcurrentHashMap();

    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session session;

    //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineUser;

    //此处是解决无法注入的
    private static ApplicationContext applicationContext;

    private WebSocketService webSocketService;

    //当前用户id
    private Long userId;

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(@PathParam("userId") Long userId, Session session) {
        onlineUser++;
        this.session = session;
        this.userId = userId;
        //把自己的信息加入map
        webSocketmap.put(userId, this);
        System.out.println("有新连接加入!当前在线人数为:" + onlineUser);
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        onlineUser--;
        System.out.println("websocket close");
    }

    /*
     *收到客户端消息
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("收到客户端消息:" + message);
        webSocketService=applicationContext.getBean(WebSocketService.class);
        webSocketService.setUserStatus(1L,"online");
        sendMessageTo("你好", 1L);
    }

    //    /*
//     *服务端发生异常
//     */
//    @OnError
//    public void onError(Session session, Throwable throwable) {
//        System.out.println("服务端发生异常");
//    }
    //发送消息
    public void sendMessageTo(String message, Long userId) {
        for (WSController item : webSocketmap.values()) {
            if (item.userId.equals(userId)) {
                item.session.getAsyncRemote().sendText(message);
            }
        }
    }

    //向所有在线用户通知
    public void sendMessageAll(String message, String FromUserName) throws IOException {
        for (WSController item : webSocketmap.values()) {
            item.session.getAsyncRemote().sendText(message);
        }
    }


    public static synchronized int getOnlineUser() {
        return onlineUser;
    }

    public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

}

你可能感兴趣的:(日长记录,Springboot,im)