因為做通信項目的時候,需要實時獲取每個分機的當前狀態,發現websocket還不錯,只是對瀏覽器的要求比較高,
針對特定用戶推送消息,網上有一些
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator{
@Override
public void modifyHandshake(ServerEndpointConfig config,HandshakeRequest request,HandshakeResponse response){
HttpSession httpSession = (HttpSession)request.getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(),httpSession);
}
}
//@ServerEndpoint(value="/websocket",configurator=GetHttpSessionConfigurator.class)
在open方法加入參數,進行實現,最后發現獲取不到session,為null。 然后通過他們說的在監聽器內,把session也加入,最后可以獲取session,卻發現用戶的session會改變
兩個httpSession 不是一個值。實在沒辦法用啦下面的法子。。。。。。。。
實際點,代碼奉上:
jsp:
<script type="text/javascript">
var BASE_PATH="${pageContext.request.contextPath}";//項目名稱
var iphost="${iphost}";//服務器ip+端口
var session="${session}";//針對特定用戶推送時,實在找不到方法只能笨點,把每個用戶放去緩存,通過后台闖入頁面
var ws;
$(function(){
ws = new WebSocket("ws://"+iphost+"/${pageContext.request.contextPath }/websocket?"+session);
ws.onopen = function(evn){
//console.log(evn);
};
ws.onmessage = function(evn){
var data = evn.data+'';
//console.log(data);
if(data.indexOf("掛機原因")>-1){
UIkit.modal.alert(evn.data);
}else if(data.indexOf("來電彈屏")>-1){
var tpphone=data.replace("來電彈屏", "");
var rw=document.body.clientWidth-800;
var rh=document.body.clientHeight-600;
window.open(BASE_PATH+'/main/asterisktest/bomb?phone='+tpphone,"","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=800, height=600,top="+rh+", left="+rw);
}else{
var json=eval('('+data+')');
var div = $("#"+json["id"]);
div.html(json["statusname"]);
}
};
ws.onclose = function(evn){
//console.log("關閉");
};
ws.onError=function(evn){
// console.log('Error occured: ' + evt.data);
};
});
</script>
java:
//@ServerEndpoint(value="/websocket",configurator=GetHttpSessionConfigurator.class)
@ServerEndpoint(value="/websocket")
public class WebSocket {
public WebSocket(){
}
//給所有的用戶推送消息
public static void broadcastAll(JSONObject jsono){
ListIterator it=WEBApp.AppSession.listIterator();
while(it.hasNext()){
try {
((Session)it.next()).getBasicRemote().sendText(jsono.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
//給指定用戶發送消息
public static void broadcast(String key,String value){
MapCacheUtil mapc=MapCacheUtil.getInstance();
try {
if(mapc.getWebsession(key)!=null){
mapc.getWebsession(key).getBasicRemote().sendText(value);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@OnOpen//,EndpointConfig config
public void onopen(Session session){
System.out.println("連接成功");
MapCacheUtil mapc=MapCacheUtil.getInstance();
mapc.putWebsession(session.getQueryString(), session);
try {
WEBApp.AppSession.add(session);
session.getBasicRemote().sendText("hello client...");
} catch (IOException e) {
e.printStackTrace();
}
}
@OnClose
public void onclose(Session session){
WEBApp.AppSession.remove(session);
System.out.println(session.getId()+"close....");
}
@OnMessage
public void onsend(Session session,String msg){
try {
session.getBasicRemote().sendText(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
//保存所有Websocket Session 的值
public class WEBApp {
public static Vector<javax.websocket.Session>
AppSession=new Vector<javax.websocket.Session>();
}
也沒什么經驗,不足的大家提出建議,起碼功能實現啦。。