Network.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. class Cursor implements hxbit.NetworkSerializable {
  2. @:s var color : Int;
  3. @:s public var uid : Int;
  4. @:s public var x(default, set) : Float;
  5. @:s public var y(default, set) : Float;
  6. var net : Network;
  7. var bmp : h2d.Graphics;
  8. public function new( color, uid=0 ) {
  9. this.color = color;
  10. this.uid = uid;
  11. init();
  12. x = 0;
  13. y = 0;
  14. }
  15. public function networkAllow( op : hxbit.NetworkSerializable.Operation, propId : Int, client : hxbit.NetworkSerializable ) : Bool {
  16. return client == this;
  17. }
  18. function set_x( v : Float ) {
  19. if( v == x ) return v;
  20. if( bmp != null ) bmp.x = v;
  21. return this.x = v;
  22. }
  23. function set_y( v : Float ) {
  24. if( bmp != null ) bmp.y = v;
  25. return this.y = v;
  26. }
  27. public function toString() {
  28. return "Cursor " + StringTools.hex(color, 6)+(enableReplication?":ALIVE":"");
  29. }
  30. function init() {
  31. net = Network.inst;
  32. net.log("Init "+this);
  33. bmp = new h2d.Graphics(net.s2d);
  34. bmp.beginFill(color, 0.5);
  35. bmp.drawCircle(0, 0, 10);
  36. bmp.beginFill(color);
  37. bmp.drawCircle(0, 0, 5);
  38. enableReplication = true;
  39. var i = new h2d.Interactive(10, 10, bmp);
  40. i.x = i.y = -5;
  41. i.isEllipse = true;
  42. i.onClick = function(_) blink( 2 + Math.random() * 2 );
  43. }
  44. @:rpc function blink( s : Float ) {
  45. bmp.scale(s);
  46. net.event.waitUntil(function(dt) {
  47. bmp.scaleX *= Math.pow(0.9, dt * 60);
  48. bmp.scaleY *= Math.pow(0.9, dt * 60);
  49. if( bmp.scaleX < 1 ) {
  50. bmp.scaleX = bmp.scaleY = 1;
  51. return true;
  52. }
  53. return false;
  54. });
  55. }
  56. public function alive() {
  57. init();
  58. // refresh bmp
  59. this.x = x;
  60. this.y = y;
  61. if( uid == net.uid ) {
  62. net.cursor = this;
  63. net.host.self.ownerObject = this;
  64. }
  65. }
  66. }
  67. //PARAM=-lib hxbit
  68. class Network extends hxd.App {
  69. static var HOST = "127.0.0.1";
  70. static var PORT = 6676;
  71. public var host : hxd.net.SocketHost;
  72. public var event : hxd.WaitEvent;
  73. public var uid : Int;
  74. public var cursor : Cursor;
  75. override function init() {
  76. event = new hxd.WaitEvent();
  77. host = new hxd.net.SocketHost();
  78. host.setLogger(function(msg) log(msg));
  79. if( !hxd.net.Socket.ALLOW_BIND ) {
  80. #if flash
  81. log("Using network with flash requires compiling with -lib air3 and running through AIR");
  82. #else
  83. log("Server not allowed on this platform");
  84. #end
  85. }
  86. try {
  87. host.wait(HOST, PORT, function(c) {
  88. log("Client Connected");
  89. });
  90. host.onMessage = function(c,uid:Int) {
  91. log("Client identified ("+uid+")");
  92. var cursorClient = new Cursor(0x0000FF, uid);
  93. c.ownerObject = cursorClient;
  94. c.sync();
  95. };
  96. log("Server Started");
  97. start();
  98. } catch( e : Dynamic ) {
  99. // we could not start the server
  100. log("Connecting");
  101. uid = 1 + Std.random(1000);
  102. host.connect(HOST, PORT, function(b) {
  103. if( !b ) {
  104. log("Failed to connect to server");
  105. return;
  106. }
  107. log("Connected to server");
  108. host.sendMessage(uid);
  109. });
  110. }
  111. }
  112. public function log( s : String, ?pos : haxe.PosInfos ) {
  113. pos.fileName = (host.isAuth ? "[S]" : "[C]") + " " + pos.fileName;
  114. haxe.Log.trace(s, pos);
  115. }
  116. function start() {
  117. cursor = new Cursor(0xFF0000);
  118. log("Live");
  119. host.makeAlive();
  120. }
  121. override function update(dt:Float) {
  122. event.update(dt);
  123. if( cursor != null ) {
  124. cursor.x = s2d.mouseX;
  125. cursor.y = s2d.mouseY;
  126. }
  127. host.flush();
  128. }
  129. public static var inst : Network;
  130. static function main() {
  131. #if air3
  132. @:privateAccess hxd.Stage.getInstance().multipleWindowsSupport = true;
  133. #end
  134. inst = new Network();
  135. }
  136. }