5.7 Minimalist mode client

The Limax extends the Websocket protocol, and supports the implementation of the minimalist mode client. The minimalist mode client can be regarded as the result that the script mode client replaces the native protocol with the extended Websocket protocol, simplifies the EndpointListener, makes relevant enhancement and is conceptually closer to the HTML5 browser. The minimalist mode client and the HTML5 browser can connect to the same port configured by Limax Websocket server simultaneously.


  • The example of the client

    The example is from the client development section. The implementation of the C++, C# and Java versions is conceptually identical. Here only the example of the C++ version implementation is given.


    #include "stdafx.h"
    #include <limax.h>
    #include <iostream>
    #include <lua.hpp>
    #include <limax.lua.h>
    #include <set>
    #include <octets.h>
    using namespace limax;
    class MyLuaApp
    {
        lua_State* L;
    public:
        MyLuaApp()
        {
            L = luaL_newstate();
            luaL_openlibs(L);
            int e = luaL_dofile(L, "callback.lua");
            if (e != LUA_OK)
            {
                std::cout << "lua load 'callback.lua' failed! " << lua_tostring(L, -1) << std::endl;
                exit(-1);
            }
            auto sehptr = LuaCreator::createScriptEngineHandle(L, -1, false, [this](int s, int e, const std::string& m){ onErrorOccured(s, e, m); });
            if (!sehptr)
                exit(-1);
            lua_pop(L, 1);
            Endpoint::openEngine();
            Endpoint::start("127.0.0.1", 10001, "testabc", "123456", "test", sehptr);
        }
        ~MyLuaApp(){
            std::mutex mutex;
            std::condition_variable_any cond;
            std::lock_guard<std::mutex> l(mutex);
            Endpoint::closeEngine([&](){
                std::lock_guard<<std::mutex> l(mutex);
                cond.notify_one();
            });
            cond.wait(mutex);
        }
        void run()  { Sleep(2000); }
        void onErrorOccured(int errorsource, int errorvalue, const std::string& info) { std::cout << "onErrorOccured " << errorsource << " " << errorvalue << " " << info << std::endl; }
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        MyLuaApp().run();
        return 0;
    }
    

    Comparing the client development -> Lua / C ++ client section, the MyLuaApp no longer need to implement the EndpointListener interface. In addition, directly using the minimalist mode client version of the Endpoint.start to launch the connection no longer need to create the revelant EndpointConfig.


    Launch the server, run the client, and get the result.

    v100.share.MyTemporaryView.onopen table: 00403000 2 table: 00403028

    v100.share.MyTemporaryView.onchange table: 00403000 36864 _var0 Hello 36864 NEW

    v100.share.MySessionView.onchange table: 003D5360 36864 var0 Hello 36864 NEW

    v100.share.MyTemporaryView.onchange table: 00403000 36864 _var0 99999 REPLACE

    v100.share.MySessionView.onchange table: 003D5360 36864 var0 99999 REPLACE

    limax close 0

    This result is consistent with the previous versions.


  • Implementation process

    • Launch the Endpoint engine

      Java void Endpoint.openEngine()
      C# void Endpoint.openEngine()
      C++ void Endpoint::openEngine()
    • Use the application script to create ScriptEngineHandle

      Java The class limax.endpoint.script.JavaScriptHandle
      C# The class limax.endpoint.script.LuaScriptHandle
      The class limax.endpoint.script.JavaScriptHandle
      C++ The method limax::LuaCreator::createScriptEngineHandle of the four versions
      The method limax::JsCreator::createScriptEngineHandle of the four versions

      The above table lists the ScriptEngineHandle implementation currently supported by the Limax.

    • Launch the Endpoint connection

      Java limax.util.Closeable Endpoint.start(String host, int port, String username, String token, String platflag, ScriptEngineHandle handle, Executor executor)
      C# limax.util.Closeable Endpoint.start(string host, int port, string username, string token, string platflag, ScriptEngineHandle handle, Executor executor)
      C++ std::shared_ptr<limax::util::Closeable> Endpoint::start(const std::string& host, short port, const std::string& username, const std::string& token, const std::string& platflag, ScriptEngineHandlePtr handle)

      1. host, port, username, token, platflag, specify the connection's Switcher server and login parameters.

      2. handle is the ScriptEngineHandle created in the previous step.

      3. The script is executed in the specified executor. In the C++ version, the script must be executed in the UI thread and this parameter is not required.

      4. Executing the close on the returned Closeable object, the effect is equivalent to clicking the STOP button on the browser, which immediately closes the connection and calles the onclose method installed on the context of the script. The message string format gotten by the onclose is "[host language error information description]<space><error code>".

      5. When the network connection disconnects in any condition, the onclose method installed on the context of the script is called. After that, calling the close method on the returned Closeable object has no effect. Actually, the internal connection object has been released. This implementation is consistent with the browser. Clicking the STOP button on the webpage with the completion of loading or failure of loading has no effect.

      6. It should be noted that all these launch methods do not support connection timeout. If the connection timeout shorter than the OS default value is required, the own timer of the application should be supplied, and execute close method on the returned Closeable object after the timeout. This implementation is consistent with the browser, and such kind of the underlying configuration for any connection timeout can not be found on the browser.

    • Close Endpoint engine

      Java void Endpoint.closeEngine(Runnable done)
      C# void Endpoint.closeEngine(Action done)
      C++ void Endpoint::openEngine(limax::Runnable done)

      All the active Endpoint connections in the engine will be automatically closed.


  • Compare with the script mode client and HTML5 client

      Minimalist mode client Script mode client HTML5 client
    Load protocol Websocket/Ext Limax Native Protocol Websocket
    Confidentiality Support Support through runtime configuration Support/https, not support/http
    Compression Support Support through runtime configuration Not support
    Script support ScriptEngineHandle ScriptEngineHandle javascript

Prev Next