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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License.
   You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and limitations under the License.*/
// BusServer class, a package of websockets you can broadcast on (server side)

define.class(function(require, exports){

        this.atConstructor = function(){
                this.sockets = []
                this.binrpc_waiting = []
        }
        
        this.use_xhr_fallback_for_binary = true

        this.binRpcIncoming = function(binrpc){

        }

        // adds a WebSocket to the BusServer
        this.addWebSocket = function(sock, req, binrpc_outgoing, binrpc_incoming){
                this.sockets.push(sock)
                var origin = req.headers.origin
                var referer = req.headers.origin+req.url
                var remoteAddress = req.connection.remoteAddress
                
                sock.onclose = function(){
                        this.sockets.splice(this.sockets.indexOf(sock), 1)
                        sock.onclose = undefined                        
                        this.atClose(sock);
                }.bind(this)

                if(this.use_xhr_fallback_for_binary){
                        var binary_xhr = []

                        sock.onmessage = function(event){
                                var message = event.data
                                if(message.charAt(0) === '$'){
                                        binary_xhr.push(message.slice(1))
                                        return
                                }
                                for(var i = 0; i < binary_xhr.length; i++){
                                        var id = binary_xhr[i]
                                        binary_xhr[i] = binrpc_incoming[id]
                                        binrpc_incoming[id] = undefined
                                }
                                var jsonmsg = JSON.parse(message)
                                jsonmsg = define.structFromJSON(jsonmsg, binary_xhr)
                                binary_xhr.length = 0
                                jsonmsg.origin = origin
                                this.atMessage(jsonmsg, sock)
                        }.bind(this)

                        function rndhex4(){ return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1) }

                        sock.sendJSON = function(msg){
                                var binary = []
                                var newmsg = define.makeJSONSafe(msg, binary)
                                for(var i = 0; i < binary.length; i++){
                                        var data = binary[i].data

                                        var rpcrandom = rndhex4()+rndhex4()+rndhex4()+rndhex4()+rndhex4()+rndhex4()+rndhex4()+rndhex4()
                                        binrpc_outgoing[rpcrandom] = {
                                                data:data,
                                                remoteAddress:remoteAddress
                                        }

                                        sock.send("$"+rpcrandom)
                                }
                                var jsonmsg = JSON.stringify(newmsg)
                                sock.send(jsonmsg)
                        }
                }        
                else{
                        sock.makeJSONSocket()
                        sock.atJSONMessage = function(jsonmsg){
                                jsonmsg.origin = origin
                                this.atMessage(jsonmsg, sock)
                        }.bind(this)
                }

                this.atConnect(sock)
        }

        // called when a new message arrives
        this.atMessage = function(message, socket){
        } 
        
        // called when a client disconnects
        this.atClose = function(socket){
        }

        // Called when a new socket appears on the bus
        this.atConnect = function(message, socket){
        }

    // Send a message to all connected sockets
        this.broadcast = function(message, ignore){
                for(var i = 0;i<this.sockets.length;i++){
                        var socket = this.sockets[i]
                        if(socket !== ignore) socket.sendJSON(message)
                }
        }

        // close all sockets
        this.closeAll = function(){
                for(var i = 0; i < this.sockets.length; i++){
                        this.sockets[i].close()
                }
                this.sockets = []
        }
})