File: jsSocket.as

package info (click to toggle)
mongrel2 1.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,976 kB
  • sloc: ansic: 39,083; python: 2,833; sql: 1,555; sh: 467; makefile: 360; asm: 189; yacc: 145; php: 73; awk: 28; sed: 5
file content (46 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download | duplicates (10)
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
import flash.external.ExternalInterface;

class jsSocket {
  private var sock:XMLSocket;
  private var id:String;

  private function calljs(type, data) {
    ExternalInterface.call('jsSocket.callback', this.id, type, data);
  }

  public function jsSocket(id) {
    this.id = id;

    ExternalInterface.addCallback('open',  this, open);
    ExternalInterface.addCallback('send',  this, send);
    ExternalInterface.addCallback('close', this, close);

    this.calljs('onLoaded', true);
  }

  public function open(host, port) {
    System.security.loadPolicyFile('xmlsocket://' + host + ':' + port);
    sock = new XMLSocket();
    
    var self = this;
    sock.onConnect = function(s) { self.calljs('onOpen', s); }
    sock.onData    = function(d) { self.calljs('onData', d); }
    sock.onClose   = function( ) { self.calljs('onClose');   }

    return sock.connect(host, port);
  }

  public function send(data) {
    if (data != 'null') // calling send() from js with no arguments sets data == 'null'
      return sock.send(data);
  }

  public function close() {
    sock.close();
    sock.onClose();
  }

  static function main(mc) {
    _root.jsSocket = new jsSocket(_root.id);
  }
}