Skip to content
Li, Xizhi edited this page Jan 2, 2018 · 6 revisions

NPLWebSocketServer

NPL TCP connection can be dynamically switched to support the rfc6455 web socket protocol. One can use json or any text/binary format to transfer data between client and a NPL web server.

WebSocket Background Knowledge

Writing a NPL server page

  • Registering a npl file to receive message
-- -20 is a predefined file id for web socket
 NPL.AddPublicFile("Mod/NplWebSocketSample/main.lua", -20);
  • Receiving message
--Mod/NplWebSocketSample/main.lua
local function activate()
   if(msg and msg.nid)then
       local user_id = msg.nid; -- user id
       local json = msg[1];
       local out={};
       -- assume the data is in json, but you can transmit in any format you like
       if(NPL.FromJson(json, out)) then
           -- received msg from client
           local received_msg = out[1];
       end
   end
end
NPL.this(activate)
  • Sending message from server to client
local json_from = commonlib.Json.Encode(msg); -- the type of msg is string/number/table
-- nid is the user id
NPL.activate(string.format("%s:websocket",nid),json_from);
  • Using http to handshake Before a server can turn an NPL http connection into a web socket connection, it must authenticate the client via http message, like below. The code is written in standard NPL web server page file.
--nplsocketsample.page
NPL.load("(gl)script/ide/System/Encoding/sha1.lua");
local Encoding = commonlib.gettable("System.Encoding");
if(is_ajax()) then
	add_action('wp_ajax_handshake', function()

        local user_id = request:get("user_id");
        if(not user_id or user_id == "")then
            user_id = "leio"; -- a test id
        end
        local key = request:header("Sec-WebSocket-Key");

        if(key and key ~= "")then
            response.headers = {};
            key = key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
            key = Encoding.sha1(key,"base64");
            response:add_header("status", "101 Switching Protocols");
            response:set_header("Connection", "Upgrade");
            response:set_header("Upgrade", "websocket");
            response:set_header("Sec-WebSocket-Accept", key);
            response:send_headers();

            -- a temp (unauthenticated) tcp connection id in npl server
            local tid = request.nid;
            -- quick authentication
            NPL.accept(tid, user_id);
            -- Change protocol type to 1 (websocket) protocol. default 0 is npl protocol.
            NPL.SetProtocol(user_id,1);
            return;
        end
    end)
    return
end
  • Changing transmission protocol
--change incomming connection's protocol to websocket
NPL.SetProtocol(user_id,1);
--change incomming connection's protocol to npl
NPL.SetProtocol(user_id,0);

Writing a client request (Javascript)

  • Create an instance of websocket
//@param nplsocketsample:it is a name of npl web page
//@param action:a name for ajax callback
//@user_id:a unique id for identify whose connection can be accepted or refused 
 var ws = new WebSocket("ws://localhost:8099/ajax/nplsocketsample?action=handshake&user_id=" + user_id);
  • now send message to server
var msg = "Hello World!";
ws.send(JSON.stringify(msg));

Code Examples

image

Clone this wiki locally