@@ -2,74 +2,83 @@ package io.colyseus;
2
2
3
3
import haxe .io .Bytes ;
4
4
import org .msgpack .MsgPack ;
5
-
6
5
import haxe .net .WebSocket ;
7
6
import haxe .net .WebSocket .ReadyState ;
8
-
9
7
#if !haxe4
10
- #if neko
11
- import neko .vm .Thread ;
12
- #elseif hl
13
- import hl .vm .Thread ;
14
- #elseif cpp
15
- import cpp .vm .Thread ;
16
- #end
8
+ #if neko
9
+ import neko .vm .Thread ;
10
+ #elseif hl
11
+ import hl .vm .Thread ;
12
+ #elseif cpp
13
+ import cpp .vm .Thread ;
14
+ #end
17
15
#elseif sys
18
- import sys .thread .Thread ;
16
+ import sys .thread .Thread ;
19
17
#end
20
18
19
+ @:keep
21
20
class Connection {
22
- public var reconnectionEnabled : Bool = false ;
23
-
24
- private var ws : WebSocket ;
25
-
26
- // callbacks
27
- public dynamic function onOpen (): Void {}
28
- public dynamic function onMessage (bytes : Bytes ): Void {}
29
- public dynamic function onClose (data : Dynamic ): Void {}
30
- public dynamic function onError (message : String ): Void {}
31
-
32
- private static var isRunnerInitialized : Bool = false ;
33
-
34
- public function new (url : String ) {
35
- this .ws = WebSocket .create (url );
36
- this .ws .onopen = function () {
37
- this .onOpen ();
38
- }
39
-
40
- this .ws .onmessageBytes = function (bytes ) {
41
- this .onMessage (bytes );
42
- }
43
-
44
- this .ws .onclose = function (? e : Dynamic ) {
45
- this .onClose (e );
46
- }
47
-
48
- this .ws .onerror = function (message ) {
49
- this .onError (message );
50
- }
51
-
52
- #if sys
53
- Thread .create (function () {
54
- while (true ) {
55
- this .ws .process ();
56
-
57
- if (this .ws .readyState == ReadyState . Closed ) {
58
- trace (" WebSocket connection has been closed, stopping the thread!" );
59
- break ;
60
- }
61
-
62
- Sys .sleep (.01 );
63
- }
64
- });
65
- #end
66
- }
67
-
68
- public function send (data : Bytes ) {
69
- return this .ws .sendBytes (data );
70
- }
71
-
72
- public function close () {
73
- this .ws .close ();
74
- }
21
+ public var reconnectionEnabled : Bool = false ;
22
+
23
+ public var _isOpen (get , never ): Bool ;
24
+
25
+ @:getter (isOpen )
26
+ function get__isOpen (): Bool {
27
+ return (this .ws .readyState == ReadyState . Open );
28
+ }
29
+
30
+ private var ws : WebSocket ;
31
+
32
+ // callbacks
33
+ public dynamic function onOpen (): Void {}
34
+
35
+ public dynamic function onMessage (bytes : Bytes ): Void {}
36
+
37
+ public dynamic function onClose (data : Dynamic ): Void {}
38
+
39
+ public dynamic function onError (message : String ): Void {}
40
+
41
+ private static var isRunnerInitialized : Bool = false ;
42
+
43
+ public function new (url : String ) {
44
+ this .ws = WebSocket .create (url );
45
+ this .ws .onopen = function () {
46
+ this .onOpen ();
47
+ }
48
+
49
+ this .ws .onmessageBytes = function (bytes ) {
50
+ this .onMessage (bytes );
51
+ }
52
+
53
+ this .ws .onclose = function (? e : Dynamic ) {
54
+ this .onClose (e );
55
+ }
56
+
57
+ this .ws .onerror = function (message ) {
58
+ this .onError (message );
59
+ }
60
+
61
+ #if sys
62
+ Thread .create (function () {
63
+ while (true ) {
64
+ this .ws .process ();
65
+
66
+ if (this .ws .readyState == ReadyState . Closed ) {
67
+ trace (" WebSocket connection has been closed, stopping the thread!" );
68
+ break ;
69
+ }
70
+
71
+ Sys .sleep (.01 );
72
+ }
73
+ });
74
+ #end
75
+ }
76
+
77
+ public function send (data : Bytes ) {
78
+ return this .ws .sendBytes (data );
79
+ }
80
+
81
+ public function close () {
82
+ this .ws .close ();
83
+ }
75
84
}
0 commit comments