Skip to content

Instantly share code, notes, and snippets.

@jonasmalacofilho
Created April 15, 2014 11:36
Show Gist options
  • Save jonasmalacofilho/10725113 to your computer and use it in GitHub Desktop.
Save jonasmalacofilho/10725113 to your computer and use it in GitHub Desktop.
Neko client for neko ThreadRemotingServer example
import haxe.remoting.Context;
import haxe.remoting.HttpConnection;
import haxe.remoting.SocketConnection;
import haxe.remoting.SocketProtocol;
import neko.vm.Thread;
import sys.net.Host;
import sys.net.Socket;
import Sys.println;
class Mini2 {
static
var SERVER_NAME = "localhost";
static
var SERVER_PORT = 40000;
static
function main() {
var stdTrace = haxe.Log.trace;
haxe.Log.trace = function ( v, ?p ) {
stdTrace( v, p );
// println( haxe.CallStack.toString( haxe.CallStack.callStack() ) );
};
var server = new Server();
Thread.create( server.run.bind( SERVER_NAME, SERVER_PORT ) );
Sys.sleep( .2 );
var conn = new Socket();
conn.connect( new Host( SERVER_NAME ), SERVER_PORT );
var rconn = SocketConnection.create( conn );
rconn.setErrorHandler( function ( v ) println( "[client][error] "+v ) );
var api = rconn.remoting;
var onResult = function ( v:Dynamic ) println( "[self client][response] "+v );
api.time.call([],onResult);
api.platform.call([],onResult);
api.getEnv.call([ "HAXEPATH" ],onResult);
Sys.sleep( .2 );
conn.close();
Sys.sleep( 1 );
Sys.exit( 0 );
}
}
private
class Server extends neko.net.ThreadRemotingServer {
override public dynamic
function initClientApi( cnx:SocketConnection, ctx:Context ):Void {
ctx.addObject( "remoting", new RemotingApi() );
cnx.setErrorHandler( function ( v ) println( "[server][error] "+v ) );
}
override public
function clientConnected( s:Socket ):SocketConnection {
println( "[server][message] Client connected: "+s.peer() );
return super.clientConnected( s );
}
override public
function run( host, port ):Void {
println( "[server][message] Starting server..." );
super.run( host, port );
}
}
private
class RemotingApi {
public
function new() {
// ---
}
public
function time() {
var ret = Date.now();
trace( ret );
return ret;
}
public
function platform() {
var ret = Sys.systemName();
trace( ret );
return ret;
}
public
function getEnv( name ) {
var ret = Sys.getEnv( name );
trace( ret );
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment