1. 程式人生 > >對於Spring對websocket的屬性注入失敗問題,困擾我一天,最後終於解決了

對於Spring對websocket的屬性注入失敗問題,困擾我一天,最後終於解決了

首先匯入包必須的:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-websocket</artifactId>
	    <version>4.3.5.RELEASE</version>
	</dependency>

以及一個websocket-api.jar


下面看程式碼

package com.sanker.webSocket;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import org.hibernate.SessionFactory;
import org.springframework.web.socket.server.standard.SpringConfigurator;

import com.sanker.Bean.player.Player;
import com.sanker.Dao.Impl.player.PlayerDaoImpl;
import com.sanker.util.JsonUtil;
@ServerEndpoint(value="/websocket/{playerId}",configurator = SpringConfigurator.class)
public class WebSocketController {
	
	@Resource
	private PlayerDaoImpl playerDao;
	
	@Resource()
	private SessionFactory sessionFactory;
	/**
	 * 用來存放所有的線上使用者
	 */
	private static final Map<String,WebSocketController> connections = new LinkedHashMap<String, WebSocketController>();
	
	/**
	 * 獲取使用者傳過來的使用者資訊
	 */
	private Session session;
	
	/**
	 * 連線websocket成功
	 * @param playerId
	 * @param session
	 */
	@OnOpen
	public void onOpen(@PathParam("playerId") String playerId, Session session){
		this.session = session;
		System.out.println(this.session.toString());
		System.out.println(playerId+"打開了連線!!!");
		connections.put(playerId, this);
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("title", "pId_check");
		msg.put("result", "success");
		sendMessage(playerId, JsonUtil.getJsonMap(msg));
		
		/***********  下面的問題  *************/
		System.out.println(playerDao);
		Player playerInfo = playerDao.getPlayerById(playerId);
		System.out.println(playerInfo);
		Map<String, Object> playerInfoMap = new HashMap<String, Object>();
		playerInfoMap.put("title", "playerInfo");
		playerInfoMap.put("info", playerInfo);

		//sendMessage(playerId, JsonUtil.getJson(playerInfoMap));
	}

	/**
	 * 接受客服端傳送的訊息
	 * @param playerId 傳送訊息的ID
	 * @param message 訊息
	 * @param session
	 */
	@OnMessage
	public void OnMessage(@PathParam("playerId") String playerId, String message, Session session){
		System.out.println(message);
		sendMessage(playerId, "收到訊息了");
	}
	
	/**
	 * 向客戶端傳送訊息
	 * @param playerId 當前使用者ID
	 * @param message 訊息內容
	 */
	public void sendMessage(String playerId,String message) {
		try {
			if (connections.containsKey(playerId)) {
				this.session.getBasicRemote().sendText(message);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 使用者關閉連線
	 * @param playerId
	 * 移除當前ID的session
	 */
	@OnClose
	public void onClose(@PathParam("playerId") String playerId) {
		System.out.println(playerId + " 已掉線!");
		connections.remove(playerId);
	}
	/**
	 * 傳送錯誤!!!
	 * @param playerId
	 * @param session
	 * @param error
	 * 移除當前ID的session
	 */
	@OnError
	public void onError(@PathParam("playerId") String playerId, Session session, Throwable error) {
		System.out.println("websocket onerror - " + playerId);
		connections.remove(playerId);
		error.printStackTrace();
	}

}

主要在@ServerEndpoint設定: configurator = SpringConfigurator.class 

意思是可以Spring注入,就這麼簡單