WebSocket Some simple page push to use

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.