Because the communication project, need to get the current status of each extension in real-time, found that WebSocket is also good, knowledge of the browser requirements are relatively high,
For specific user push messages, there are some online
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)
The open method joins the parameter, implements it, and finally finds that the session is not obtained, and is null. Then through what they say in the listener, the session is added, and finally can get the session, but found that the user's session will change
Two httpsession is not a value, put to put in also disappeared. There is no way to use the following method ...
The actual point, the code is served:
Jsp:
<script type= "Text/javascript" >
var base_path= "${pagecontext.request.contextpath}";//Project name
var iphost= "${iphost}";//server ip+ port
var session= "${session}";//For a specific user push, really can not find a method only stupid point, put each user cache, through the background break into the page
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 ("Hang Machine Reason") >-1) {
UIkit.modal.alert (Evn.data);
}else if (data.indexof ("Call Bounce Screen") >-1) {
var tpphone=data.replace ("Call Bounce Screen", "");
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 ("Off");
};
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 () {
}
Push messages to all users
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 ();
}
}
}
Send a message to a specified user
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 ("Connection succeeded");
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 ();
}
}
Save values for all WebSocket Session
public class WEBApp {
public static vector<javax.websocket.session>
Appsession=new vector<javax.websocket.session> ();
}
There is no experience, not enough people to make suggestions, at least the implementation of the function.
WebSocket Some simple page push to use