How to use WebSockets refresh multi-window for play framework 1.2.7
- by user2468652
My code can work.But only refresh a page of one window.
If I open window1 and window2 , both open websocket connect.
I keyin word "test123" in window1, click sendbutton.
Only refresh window1.
How to refresh window1 and window2 ?
Client
<script>
window.onload = function() {
document.getElementById('sendbutton').addEventListener('click', sendMessage,false);
document.getElementById('connectbutton').addEventListener('click', connect, false);
}
function writeStatus(message) {
var html = document.createElement("div");
html.setAttribute('class', 'message');
html.innerHTML = message;
document.getElementById("status").appendChild(html);
}
function connect() {
ws = new WebSocket("ws://localhost:9000/ws?name=test");
ws.onopen = function(evt) {
writeStatus("connected");
}
ws.onmessage = function(evt) {
writeStatus("response: " + evt.data);
}
}
function sendMessage() {
ws.send(document.getElementById('messagefield').value);
}
</script>
</head>
<body>
<button id="connectbutton">Connect</button>
<input type="text" id="messagefield"/>
<button id="sendbutton">Send</button>
<div id="status"></div>
</body>
Play Framework WebSocketController
public class WebSocket extends WebSocketController {
public static void test(String name) {
while(inbound.isOpen()) {
WebSocketEvent evt = await(inbound.nextEvent());
if(evt instanceof WebSocketFrame) {
WebSocketFrame frame = (WebSocketFrame)evt;
System.out.println("received: " + frame.getTextData());
if(!frame.isBinary()) {
if(frame.getTextData().equals("quit")) {
outbound.send("Bye!");
disconnect();
} else {
outbound.send("Echo: %s", frame.getTextData());
}
}
}
}
}
}