使用正常的注入
@Autowired
public static SocketMsgService socketMsgService;
运行出来service为null,原因是因为spring容器管理bean是一个单例,而每次创建的websocket都不一样,而spring默认是注入一次,所以导致了null现像
**
完整代码*
WebSocket里面的代码
package com.example.demo.endpoint;
import com.example.demo.domain.SocketMsg;
import com.example.demo.service.SocketMsgService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @author chen
* @date 2019/10/26
* @email 15218979950@163.com
* @description websocket的具体实现类
* 使用springboot的唯一区别是要@Component声明,而使用独立容器是由容器自己管理websocket的,
* 但在springboot中连容器都是spring管理的。
* 虽然@Component默认是单例模式的,但springboot还是会为每个websocket连接初始化一个bean,
* 所以可以用一个静态set保存起来
*/
@ServerEndpoint(value = "/websocket/{nickname}")
@Component
public class WebSocket {
private String nickname;
private Session session;
/**
*
*/
public static SocketMsgService socketMsgService;
//用来存放每个客户端对应的MyWebSocket对象。
private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<WebSocket>();
//与某个客户端的连接会话,需要通过它来给客户端发送数据
//用来记录sessionId和该session进行绑定
private static Map<String, Session> map = new HashMap<String, Session>();
// private static Map<String, SocketMsgService> mapSocketMsgService = new HashMap<String, SocketMsgService>();
/**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session, @PathParam("nickname") String nickname) {
Map<String,Object> message=new HashMap<String, Object>();
this.session = session;
this.nickname = nickname;
map.put(session.getId(), session);
// mapSocketMsgService.put()
webSocketSet.add(this);//加入set中
System.out.println("有新连接加入:" + nickname + ",当前在线人数为" + webSocketSet.size());
//this.session.getAsyncRemote().sendText("恭喜" + nickname + "成功连接上WebSocket(其频道号:" + session.getId() + ")-->当前在线人数为:" + webSocketSet.size());
message.put("type",0); //消息类型,0-连接成功,1-用户消息
message.put("people",webSocketSet.size()); //在线人数
message.put("name",nickname); //昵称
message.put("aisle",session.getId()); //频道号
this.session.getAsyncRemote().sendText(new Gson().toJson(message));
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
webSocketSet.remove(this); //从set中删除
System.out.println("有一连接关闭!当前在线人数为" + webSocketSet.size());
}
/**
* 收到客户端消息后调用的方法
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session, @PathParam("nickname") String nickname) {
System.out.println("来自客户端的消息-->" + nickname + ": " + message);
//从客户端传过来的数据是json数据,所以这里使用jackson进行转换为SocketMsg对象,
// 然后通过socketMsg的type进行判断是单聊还是群聊,进行相应的处理:
ObjectMapper objectMapper = new ObjectMapper();
SocketMsg socketMsg;
try {
socketMsg = objectMapper.readValue(message, SocketMsg.class);
//获取时间
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final String dateStr = format.format(new Date());
ParsePosition pos = new ParsePosition(0);
socketMsg.setCreateDate(format.parse(dateStr,pos));
if (socketMsg.getType() == 1) {
//单聊.需要找到发送者和接受者.
socketMsg.setFromUser(session.getId());//发送者.
Session fromSession = map.get(socketMsg.getFromUser());
Session toSession = map.get(socketMsg.getToUser());
//发送给接受者.
if (toSession != null) {
//发送给发送者.
Map<String,Object> m=new HashMap<String, Object>();
m.put("type",1);
m.put("name",nickname);
m.put("msg",socketMsg.getMsg());
//已读
socketMsg.setIsRead("1");
socketMsgService.insert(socketMsg);
fromSession.getAsyncRemote().sendText(new Gson().toJson(m));
toSession.getAsyncRemote().sendText(new Gson().toJson(m));
} else {
//未读
socketMsg.setIsRead("0");
socketMsgService.insert(socketMsg);
//发送给发送者.
fromSession.getAsyncRemote().sendText("系统消息:对方不在线或者您输入的频道号不对");
}
} else {
//群发消息
broadcast(nickname + ": " + socketMsg.getMsg());
}
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发生错误时调用
*/
@OnError
public void onError(Session session, Throwable error) {
System.out.println("发生错误");
error.printStackTrace();
}
/**
* 群发自定义消息
*/
public void broadcast(String message) {
for (WebSocket item : webSocketSet) {
item.session.getAsyncRemote().sendText(message);//异步发送消息.
}
}
}
编写配置类
@Configuration
public class WebsocketConfiguration {
//添加下面配置 在socket引入socketUserService
@Autowired
public void socketUserService(SocketMsgService socketUserService){
WebSocket.socketMsgService= socketUserService;
}
}
package com.example.demo.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
/**
* 注入ServerEndpointExporter,
* 这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
最后两个配置类可以合并在一起的,成功解决了service为null的问题