GitHub Documentation Patreon donate button

Stencyl (HaXe) Extension

  • Haxe Client and Server on v0.10

    Using the externs by Serjek I was able to create a very Simple Colyseus Server with a StateHandler Room that handles string data. The data is send to the clients and everything works as it supposed to.

    But only for HTML5 !!!

    I again have lots of problems to get it to work on anything else than HTML5.

    Here are the (non-stencyl) HaxeToolkit steps that I made:

    set HAXE_PATH=c:\HaxeToolkit\haxe
    set NEKOPATH=c:\HaxeToolkit\neko
    haxelib setup c:\HaxeToolkit\haxe\lib
    haxelib --global update haxelib

    haxelib is up to date
    haxelib install openfl
    haxelib run openfl setup
    haxelib run lime setup
    Up to Date

    Get GIT from :

    git clone
    cd \HaxeToolkit\colyseus-hx\example\openfl\Source
    notepad Main.hx

    Edit: the ws connection so that it points to the docker server
    Also Edit the this.client.join line on number 55:

            // = this.client.join("state_handler", [], State); = this.client.join("state_handler", new Map<String,Dynamic>(), State);

    Build the project:

    cd \HaxeToolkit\colyseus-hx\example\openfl
    haxelib install haxe-ws
    haxelib set hxcpp 3.2.94


    lime build project.xml html5
    lime test html5

    It works like a charm with the Serjek externs in the Colyseus Docker .

    But Android and Windows give again connection problems:

    cd \HaxeToolkit\colyseus-hx\example\openfl
    haxelib set hxcpp 4.0.8
    lime build windows
    lime test windows

    The connection is attempted but not made:

     - src/lime/utils/AssetCache.cpp  [haxe,release]
    Link: ApplicationMain.exe
       Creating library ApplicationMain.lib and object ApplicationMain.exp
    Main.hx:48: CLIENT CLOSE
    Connection.hx:66: WebSocket connection has been closed, stopping the thread!
    Main.hx:29: ERROR! timeout

    (I've tried the alterations on the haxe-ws library that I made for the 0.9 but they didn't solved it now)

    I am very curious if anyone got a different target to run with haxe other than HTML5 ??!??

  • administrator

    Hi @mdotedot, have you tried the haxe-ws fork here?

    You can install it via:

    haxelib git haxe-ws

    I've fixed the WS handshake on sys targets on this fork. Let me know if the CPP target works for you using it. Cheers!

  • @endel said in Stencyl (HaXe) Extension:

    Endel you are a true Master!

    C:\HaxeToolkit\haxe\lib>haxelib git haxe-ws
    Installing haxe-ws from
    Library haxe-ws current version is now git
    C:\HaxeToolkit\haxe\lib>haxelib list
    actuate: [1.8.9]
    box2d: [1.2.3]
    haxe-ws: [git]
    haxelib: [3.3.0]
    hxcpp: 3.2.94 [4.0.8]
    layout: [1.2.1]
    lime-samples: [7.0.0]
    lime: 7.2.1 [7.5.0]
    openfl-samples: [8.7.0]
    openfl: 8.8.0 [8.9.1]

    Now to build / test it again:

    cd \HaxeToolkit\colyseus-hx\example\openfl
    lime build windows
    lime test windows


    Can't find a secure source of random bytes. Reason: [file_open,\Device\KsecDD]

    Debugging leads me to this part

    trace("WebSocketGeneric.hx . initialize ... Before this.key 		");
            //this.key = Base64.encode(Crypto.getSecureRandomBytes(16)); // This generates the secure source of random bytes
    		this.key = Base64.encode("ABCDEFGHIJKLMNOP"));
    trace("initialize key : "+this.key);

    After this ofString-"16-bytes" the error was gone and hand-shake was made!!!

    Windows and Android publication worked out of the box.

    I will test iOS/Mac later this week!

  • administrator

    Thanks for testing @mdotedot, that's nice. The problem is on getSecureRandomBytes method on Windows (unfortunately I don't have a Windows machine available for testing)

    This method is used to generate the Sec-WebSocket-Key header for handshake. The error comes from this line. I've borrowed the implementation from here.

    Would be great if you find a way to generate secure random bytes on Windows environment! Cheers!

  • The wikipedia linked in the sourcecode of Crypto.hx mentioned :
    "but reading the special file \Device\KsecDD does not work as in UNIX"

    So I tried another crypto number generator:

    haxelib install trandom

    Edit the project.xml to include this haxelib

    notepad \HaxeToolkit\haxe\lib\haxe-ws\git\src\haxe\net\Crypto.hx
    Change this:

    	 #if windows
    			var randomValue = trandom.TargetRandom.random();
    			var bytes =;
    		        bytes.setInt32(0, randomValue);
    			return bytes;
                        var input ="\\Device\\KsecDD");

    This worked when I did a lime build & test for windows.

    When I tried to incorporate the trandom library into Stencyl it gave me problems. There is a define done that is going to add a build.xml file to the publication method and Stencyl does not handle that.

    I opted for another approach for the windows build from in Stencyl:

    		#if windows
    					// This will not work
                        var input ="\\Device\\KsecDD");


    public static function getRandomString(length:Int, ?charactersToUse = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"):String
    		var str = "";
    		for (i in 0...length){
    			str += charactersToUse.charAt( Math.floor((Math.random() *  ( % (charactersToUse.length) ) )));
    		return str;
    	} //getRandomString

    It is not as strong as the crypto generator but at least it works.

    Confirmed v0.10 & Serjek Externs with Simple Stencyl Extension (Kind of like the NyanCat demo)

    • HTML5
    • Windows
    • Android
    • iOS Simulator
    • iOS on device
    • Macintosh OSX
    • (Oracle) Linux

  • administrator

    Awesome, thanks for sharing @mdotedot! I've just added your workaround for Windows into the haxe-ws fork:

  • The past days I've worked on the Colyseus v0.10 framework and the Haxe externs from Serjek. As well as creating a docker that will run the server and will be supplied to the Stencyl Users so they can run the server on their own.

    Since Endel has provided a haxe-ws fork ( ) the system is really stable.

    I've started with the core room-types:

    • Lobby
    • Raw
    • Turn
    • Lock

    Lobby is where all players are signed in automatically. The playernames are stored and can be used in other rooms.

    Raw Room is for games that don't need server logic. Think of games that are split screen and don't interact with eachother.

    Turn Room is for turnbased games.

    Lock system is where the server has a lock on coordinates/areas/objects and you request a lock to alter the data. Think of RPGs, puzzle games

    In the future I want to have a Box2D Server collision interaction, but that will propably take a long time to figure out.

    Currently a proof-of-concept has been made for the raw-room type. For the others I need to create more Stencyl blocks like request/release lock and turn (seat/next turn)

    Here is the current Stencyl palette layout:

    I've used client.getAvailableRooms

    for (room in rooms) {
    	if(room.metadata.ApplicationID == ""+colyseus.ApplicationID){

    I wonder if there can be a filter for things like this so that I don't get all the rooms and filter the applicationID out 'manually'
    At least it works, but I don't know if it slows down when there are a lot of games running on the server?
    Then again, I don't think that many stencyl-developers will run a server with different type of games.

  • With the core functionality done, the next room was much faster to create.

    RoomType: Turn Based System

    Proof Of Concept (sorry for the wall of text which is used to debug stuff)

    This leads to these new blocks in Stencyl:

    When a player leaves the system will pick the next seat as the active seat. If there is no seat after the leaving player the first available seat is selected.

    Also, the seat that becomes availabe is added to a stack for next players to join to. (Of course when the Stencyl Developer decides to allow join/rejoining of active game!)

  • The Lock System is still a good challenge. After two code-refactors I have put it on hold for now.

    Last week I've worked on an actual TurnBased system game (TicTacToe)

    [running html5 and windows publication next to eachother]

    (published for HTML5, Windows and Android; should work on the others)

    During development I started to rethink my approach of a single extension versus multiple extensions.
    I believe that a Stencyl user wants to create a specific type of game and install / enable the extension that is used for that type of game.
    This make the palette a lot cleaner. (Unfortunately Stencyl Extension Developers don't have the means to create sub-palettes or tabs)

    TODO for TurnBased System :

    • set timeout value; when a player needs to react before certain time (auto-logout)

    TODO for general :

    • make roomtype specific extensions (raw, turn, physics, lock/logic) that have still the general purpose blocks (init, join, create, leave room, etc..)

    TODO for games:

    • Make another turnbased game (Poker?)

    Also, I need to figure out the Lock/Logic room type eventually...

  • Working towards a beta release of the Colyseus Stencyl Extension.

    I had already Lock, TurnBased and Raw room-types.

    Most of the progress has been made with the Client-As-A-Server concept.
    I know that the best way to handle multiplayer is on the server-side.
    But many of the Stencyl users are not comfortable writing the server-side logic.

    Therefore I attempted for a Client-Server-Relay kind of thing.
    The collision and logic is handled on the client side with one of the clients acting as the server.
    On that client the real physics objects are hidden and only the data that is send to all players is displayed.

    These are the things that are currently made with the Client-As-A-Server approach:

    alt pong

    Left Window is a Windows executable
    Right Window is a web-page

    alt physics

    I hope to do some more beta-testing with other Stencyl users to gain more information about this approach.


© 2020 Endel Dreyer