20. Client Slide: JavaScript for
WebScoket
2014/3/1220
其他
Encoders與 Decoders
處理資料型別的轉換
Java Object to JSON/XML
Path 參數
把 URI 以參數方式處理
@ServerEndpoint("/chatrooms/{room-name}")
public class ChatEndpoint {
...
}
http://localhost:8080/chatapp/chatrooms/currentnews
21. Client Slide: JavaScript for
WebScoket
2014/3/1221
@ServerEndpoint("/chatrooms/{room-name}")
public class ChatEndpoint {
@OnOpen
public void open(Session session,EndpointConfig c,
@PathParam("room-name") String roomName) {
// Add the client to the chat room of their choice ...
}
}
22. Client Slide: JavaScript for
WebScoket
2014/3/1222
異常處理
@ServerEndpoint("/testendpoint")
public class TestEndpoint {
...
@OnError
public void error(Session session, Throwable t) {
t.printStackTrace();
...
}
}
23. Client Slide: JavaScript for
WebScoket
2014/3/1223
建立連線
關閉連線
委派訊息事件
var wsocket = new WebSocket("ws://localhost:8080/dukeetf2/dukeetf");
wsocket.onmessage = onMessage;
function onMessage(evt) {
var arraypv = evt.data.split(",");
document.getElementById("price").innerHTML = arraypv[0];
document.getElementById("volume").innerHTML = arraypv[1];
}
wsocket.close();