mapserver/server/static/js/WebSocketChannel.js

39 lines
927 B
JavaScript
Raw Normal View History

2019-02-01 14:46:38 +03:00
var WebSocketChannel = function(){
2019-01-29 20:08:54 +03:00
'use strict';
var wsUrl = location.protocol.replace("http", "ws") + "//" + location.host + location.pathname.substring(0, location.pathname.lastIndexOf("/")) + "/api/ws";
var listenerMap = {/* type -> [listeners] */};
2019-02-01 14:46:38 +03:00
this.addListener = function(type, listener){
2019-01-29 20:08:54 +03:00
var list = listenerMap[type];
if (!list){
list = [];
listenerMap[type] = list;
}
list.push(listener);
2019-02-01 14:46:38 +03:00
};
2019-01-29 20:08:54 +03:00
2019-02-01 14:46:38 +03:00
this.connect = function(){
2019-01-29 20:08:54 +03:00
var ws = new WebSocket(wsUrl);
ws.onmessage = function(e){
var event = JSON.parse(e.data);
//rendered-tile, mapobject-created, mapobjects-cleared
var listeners = listenerMap[event.type];
if (listeners){
listeners.forEach(function(listener){
listener(event.data);
});
}
}
ws.onerror = function(){
//reconnect after some time
setTimeout(connect, 1000);
}
};
2019-02-01 14:46:38 +03:00
};