PageSourceSearch

https://www.envafors.dk/lib/tslib/chat.js

js envafors.dk collected 2026-10-08 18:32:38 UTC 14,746 bytes, 345 lines download raw bytes

1(function ()
2{
3    function _$chat()
4    {
5        var ws = null;        
6        var delegate = {};
7        var endpoint = '/socket.ashx';
8    }
9
10    _$chat.prototype = 
11    {
12        subscribe: function (channelId, userId, instance, tagName, callback, lineSeed)
13        {
14            if (!$chat.socket.canSocket())
15            {
16                $ts.ajax({
17                    'url': '/socket.ashx' + '?channel=' + channelId,
18                    'type': 'POST',
19                    'data': { 'action': 'subscribe', 'user': userId, 'tagname': tagName, 'instance': instance, 'line': lineSeed },
20                    'complete': function (a, b) { if (callback) callback(b); }
21                });
22            }
23            else
24            {
25                if (callback) $chat.delegate.subscribe = callback;
26                $chat.socket.sendCommand(channelId, userId, tagName, 'subscribe', null);
27            }
28        },
29        unsubscribe: function (channelId, userId, instance, callback)
30        {
31            if (!$chat.socket.canSocket())
32            {
33                $ts.ajax({
34                    'url': '/socket.ashx' + '?channel=' + channelId,
35                    'type': 'POST',
36                    'data': { 'action': 'unsubscribe', 'user': userId, 'instance': instance },
37                    'complete': function (a, b) { if (callback) callback(b); }
38                });
39            }
40            else
41            {
42                if (callback) $chat.delegate.unsubscribe = callback;
43                $chat.socket.sendCommand(channelId, userId, 'unsubscribe');
44            }
45        },
46        post: function (channelId, userId, instance, text, callback)
47        {
48            if (!$chat.socket.canSocket())
49            {
50                $ts.ajax({
51                    'url': '/socket.ashx' + '?channel=' + channelId,
52                    'type': 'POST',
53                    'data': { 'action': 'post', 'user': userId, 'text': text, 'instance': instance },
54                    'complete': function (a, b) { if (callback) callback(b); }
55                });
56            }
57            else
58            {
59                $chat.delegate.post = callback;
60                $chat.socket.postMessage(channelId, userId, text);
61            }
62        },
63        notify: function (channelId, userId, instance, text, callback) {
64            if (!$chat.socket.canSocket()) {
65                $ts.ajax({
66                    'url': '/socket.ashx' + '?channel=' + channelId,
67                    'type': 'POST',
68                    'data': { 'action': 'notify', 'user': userId, 'text': text, 'instance': instance },
69                    'complete': function (a, b) { if (callback) callback(b); }
70                });
71            }
72            else {
73                //$chat.delegate.post = callback;
74                //$chat.socket.postMessage(channelId, userId, text);
75            }
76        },
77        changepost: function (channelId, userId, instance, text, line, callback) {
78            if (!$chat.socket.canSocket()) {
79                $ts.ajax({
80                    'url': '/socket.ashx' + '?channel=' + channelId,
81                    'type': 'POST',
82                    'data': { 'action': 'changepost', 'user': userId, 'text': text, 'line': line, 'instance': instance },
83                    'complete': function (a, b) { if (callback) callback(b); }
84                });
85            }
86            else {
87                //$chat.delegate.post = callback;
88                //$chat.socket.postMessage(channelId, userId, text);
89            }
90        },
91        assigntag: function (channelId, userId, instance, text, callback)
92        {
93            if (!$chat.socket.canSocket())
94            {
95                $ts.ajax({
96                    'url': '/socket.ashx' + '?channel=' + channelId,
97                    'type': 'POST',
98                    'data': { 'action': 'assigntag', 'user': userId, 'text': text, 'instance': instance },
99                    'complete': function (a, b) { if (callback) callback(b); }
100                });
101            }
102            else
103            {
104                if (callback) $chat.delegate.assigntag = callback;
105                $chat.socket.sendCommand(channelId, userId, 'assigntag', text);
106            }
107        },
108        listen: function (channelId, userId, instance, progressCallback, completeCallback, timeoutCallback)
109        {
110            var previousChunk = '';
111            $ts.ajax({
112                'url': '/socket.ashx' + '?channel=' + channelId,
113                'type': 'POST',
114                'data': { 'action': 'listen', 'user': userId, 'instance': instance },
115                'chunked': true,
116                'timeout' : 30000,
117                'progress': function (a, b)
118                {
119                    if (progressCallback)
120                    {
121                        //$ts.consoleWrite('part: ' + b);                        
122                        if (previousChunk && previousChunk != '') b = previousChunk + b;
123                        while (b.length > 0 && b[0] != '[') b = b.substring(1);
124
125                        var unused = '';
126                        var parse = function (s)
127                        {                            
128                            var stack = [];
129                            var result = '';                                                       
130                            for (var i = 0; i < s.length; i++)
131                            {
132                                if (s[i] == '[') stack.push('[');
133                                if (s[i] == ']') stack.pop();
134                                if (stack.length == 0)
135                                {
136                                    unused = s.substring(i + 1);
137                                    result = s.substring(0, i + 1);
138                                    break;
139                                }
140                            }
141                            return result;
142                        }                        
143                        var d = parse(b);
144                        if (d && d.length > 0)
145                        {
146                            progressCallback(d);
147                            previousChunk = unused;
148                        }
149                        else previousChunk = b;
150                    }
151                },
152                'complete': function (a, b) { if (completeCallback) completeCallback(); },
153                'ontimeout': function () { if (timeoutCallback) timeoutCallback(); }
154            });
155        },
156        easylisten: function (channelId, userId, instance, delegate)
157        {
158            if (!$chat.socket.canSocket())
159            {
160                var listen = function ()
161                {
162                    //$ts.consoleWrite('listening...');
163                    //if (confirm('debug?')) throw new Error();
164                    $chat.listen(channelId, userId, instance, function (d)
165                    {                        
166                        var lines = null;
167                        try
168                        {
169                            lines = eval(d);
170                        }
171                        catch (e)
172                        {
173                        	$ts.consoleDevWrite('error: ' + e.description);
174                        	$ts.consoleDevWrite(d);
175                        }
176
177                        if (lines)
178                        {                            
179                            for (var i = 0; i < lines.length; i++)
180                            {
181                                (function (idx)
182                                {
183                                    //$ts.consoleWrite(JSON.stringify(lines[idx]));
184                                    if (delegate && delegate.processMessage)
185                                    {
186                                        delegate.processMessage(lines[idx].type, lines[idx]);
187                                    }
188                                })(i);
189                            }
190                        }                        
191                    }, function ()
192                    {
193                        //$ts.consoleWrite('complete');
194                        listen();
195                    }, function ()
196                    {
197                        //$ts.consoleWrite('timout');
198                        listen();
199                    });
200                }
201
202                setTimeout(function () { listen(); }, 3000);
203            }
204            
205        },
206        socket: (function()
207        {
208            var skt = {};
209            skt.reconnects = 0;
210            skt.canSocket = function ()
211            {
212                return false;
213                return typeof (window.WebSocket) != 'undefined';                
214            }            
215
216            if (skt.canSocket())
217            {                
218                //var ws = new WebSocket('ws://developer.sbc.tangora.dev:' + Tangora.ChatPort + '/websession');
219                skt.ws = new WebSocket('ws://' + punycode.ToSafeASCII(location.hostname) + ':' + Tangora.ChatPort + '/websession');                                
220
221                skt.addSocketInterface = function(obj)
222                {
223                    obj.onopen = function ()
224                    {
225                        if ($chat.delegate && ('onopen' in $chat.delegate))
226                        {
227                            $chat.delegate.onopen();
228                        }
229                        $ts.consoleDevWrite('webSocket open');
230                    };
231                    obj.onmessage = function (evt)
232                    {
233                        //alert("About to receive data");
234                        var received_msg = evt.data;
235                        var delegate = $chat.delegate;
236                        var d = eval('(' + received_msg + ')');
237
238                        if (d.messagetype == 'command')
239                        {
240                            var command = d.command;
241                            switch (command)
242                            {
243                                case 'subscribe':
244                                    if ("subscribe" in delegate)
245                                        delegate.subscribe(d.value);
246                                    break;
247                                case 'unsubscribe':
248                                    if ("unsubscribe" in delegate)
249                                        delegate.unsubscribe();
250                                    break;
251                                case 'assigntag':
252                                    if ("assigntag" in delegate)
253                                        delegate.assigntag(d.value);
254                                    break;
255                            }
256                        }
257                        else
258                        {
259                            var lines = null;
260                            try
261                            {
262                                lines = eval(d);
263                            }
264                            catch (e)
265                            {
266                            	$ts.consoleDevWrite('parseerror: ' + d);
267                            	$ts.consoleDevWrite(e.message);
268                            }
269
270                            if (lines)
271                            {
272                                for (var i = 0; i < lines.length; i++)
273                                {
274                                    (function (idx)
275                                    {
276                                        if (delegate && delegate.processMessage)
277                                        {
278                                            delegate.processMessage(lines[idx].type, lines[idx]);
279                                        }
280                                    })(i);
281                                }
282                            }
283                            else
284                            {
285                                //subscribe
286                            }
287                        }
288                    };
289
290                    obj.onclose = function ()
291                    {
292                    	$ts.consoleDevWrite('webSocket closed');
293                        if ($chat.delegate && ('onclose' in $chat.delegate))
294                        {
295                            $chat.delegate.onclose();
296                        }
297                    };
298                }                             
299
300                skt.addSocketInterface(skt.ws);
301
302                skt.reconnect = function ()
303                {
304                    $ts.ajax({ url: '/chat.ashx', async: false });
305                    skt.ws = new WebSocket('ws://' + punycode.ToSafeASCII(location.hostname) + ':' + Tangora.ChatPort + '/websession');                                        
306                    skt.addSocketInterface(skt.ws);                                            
307                }
308
309                skt.addToMessage = function (msg, id, value)
310                {
311                    if (msg == null) msg = [];
312                    msg.push({ "id": id, "value": value });
313                    return msg;
314                }
315
316                skt.postMessage = function (channelId, userId, message)
317                {
318                    var msg = skt.addToMessage(null, 'messagetype', 'command');
319                    msg = skt.addToMessage(msg, 'command', 'post');
320                    msg = skt.addToMessage(msg, 'channel', channelId);
321                    msg = skt.addToMessage(msg, 'user', userId);
322                    msg = skt.addToMessage(msg, 'message', message);
323                    
324                    skt.ws.send(JSON.stringify(msg));                    
325                }
326
327                skt.sendCommand = function (channelId, userId, command, message)
328                {
329                    var msg = skt.addToMessage(null, 'messagetype', 'command');
330                    msg = skt.addToMessage(msg, 'command', command);
331                    msg = skt.addToMessage(msg, 'channel', channelId);
332                    msg = skt.addToMessage(msg, 'user', userId);
333                    msg = skt.addToMessage(msg, 'message', message);
334
335                    skt.ws.send(JSON.stringify(msg));                    
336                }
337                
338            }
339            
340            return skt;
341                        
342        })()    
343    }    
344    window.$chat = new _$chat();
345})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.