-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphpOverWebsocket.js
55 lines (41 loc) · 1.34 KB
/
phpOverWebsocket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var phpOverWebsocket = {
socket: null,
reconnect: null,
onopen: [],
counter: 0,
sessionid: null,
lastRequests: [],
start: function (websocketServerLocation, sessionid){
phpOverWebsocket.sessionid = sessionid;
phpOverWebsocket.socket = new WebSocket(websocketServerLocation);
phpOverWebsocket.socket.onmessage = function(event) {
var response = JSON.parse(event.data);
var request = {
getResponseHeader: function(){
return "";
}
};
phpOverWebsocket.lastRequests[response.counter].success(response.answer.split("\r\n\r\n")[1], null, request);
delete phpOverWebsocket.lastRequests[response.counter];
};
phpOverWebsocket.socket.onclose = function(){
setTimeout(function(){
phpOverWebsocket.start(websocketServerLocation, phpOverWebsocket.sessionid);
}, 1000);
};
},
send: function(data){
data.sessionid = phpOverWebsocket.sessionid;
data.counter = phpOverWebsocket.counter;
phpOverWebsocket.lastRequests[phpOverWebsocket.counter] = data;
var jsonString = JSON.stringify(data);
phpOverWebsocket.socket.send(jsonString);
phpOverWebsocket.counter++;
},
ready: function(){
if(phpOverWebsocket.socket === null)
return false;
return phpOverWebsocket.socket.readyState === 1;
}
};
//phpOverWebsocket.start('ws://localhost:8080', '<?php echo session_id(); ?>');