Advertisement
Masadow

Server side

Sep 3rd, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 1.36 KB | None | 0 0
  1. package net;
  2.  
  3. /**
  4.  * ...
  5.  * @author Masadow
  6.  */
  7. // define a typed remoting API
  8. class ClientApiImpl extends haxe.remoting.AsyncProxy<net.IClientApi> {
  9. }
  10.  
  11. // our client class
  12. class ClientData extends ServerApi {
  13.  
  14.     var api : ClientApiImpl;
  15.     var name : String;
  16.  
  17.     public function new( scnx : haxe.remoting.SocketConnection ) {
  18.         api = new ClientApiImpl(scnx.client);
  19.         (cast scnx).__private = this;
  20.     }
  21.  
  22.     public function leave() {
  23.         Server.clients.remove(this);
  24.     }
  25.  
  26.     public static function ofConnection( scnx : haxe.remoting.SocketConnection ) : ClientData {
  27.         return (cast scnx).__private;
  28.     }
  29.  
  30. }
  31.  
  32. // server loop
  33.  
  34. class Server {
  35.  
  36.     public static var clients = new List<ClientData>();
  37.  
  38.     static function initClientApi( scnx : haxe.remoting.SocketConnection, context : haxe.remoting.Context ) {
  39.         trace("Client connected");
  40.         scnx.setErrorHandler(function (error) { trace(error); } );
  41.         var c = new ClientData(scnx);
  42.         context.addObject("api", c);
  43.     }
  44.  
  45.     static function onClientDisconnected( scnx ) {
  46.         trace("Client disconnected");
  47.         ClientData.ofConnection(scnx).leave();
  48.     }
  49.  
  50.     static function main() {
  51.         var host = "localhost";
  52.         var domains = [host];
  53.         var s = new neko.net.ThreadRemotingServer(domains);
  54.         s.initClientApi = initClientApi;
  55.         s.clientDisconnected = onClientDisconnected;
  56.         trace("Starting server...");
  57.         s.run(host,3000);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement