Expand/Shrink

nopoll_ctx_set_on_msg

Definition: include nopoll.e

nopoll_ctx_set_on_msg(atom ctx, on_msg_cb, pUserData=NULL)
Description: Set a general handler to get notifications about a message received over any connection that is running under the provided context.

ctx: The context where the notification will happen
on_msg_cb: The handler to be called when an incoming message is received.
pUserData: User defined pointer that is passed in into the handler when called.
Note that the handler configured here will be overriden by the handler configured by nopoll_conn_set_on_msg().
Example:
--DEV not tested!
include nopoll.e

function listener_on_message(atom /*ctx*/, conn, pMsg, /*user_data*/)
    printf(1,"%s\nThe size of message is %d bytes\n",
             {nopoll_msg_get_payload(pMsg),
              nopoll_msg_get_payload_size(pMsg)})
    ?nopoll_conn_send_text(conn, "Hello World")
    return 0 -- (rqd for call_back, otherwise ignored)
end function
constant listener_on_message_cb = call_back(routine_id("listener_on_message"))

atom ctx = nopoll_ctx_new()
if ctx=NULL then ?9/0 end if

--DEV??
-- create a listener to receive connections on port 1234
atom listener = nopoll_listener_new(ctx, "0.0.0.0", "9999");
if not nopoll_conn_is_ok(listener) then ?9/0 end if

-- now set a handler that will be called when a message (fragment or not) is received
nopoll_ctx_set_on_msg(ctx, listener_on_message_cb, NULL)

-- now call to wait for the loop to notify events
integer error_code = nopoll_loop_wait(ctx, 0)

nopoll_ctx_unref(ctx)
See Also: nopoll_conn_set_on_msg, nopoll_conn_is_ok, nopoll_conn_is_ready, nopoll_msg_get_payload, nopoll_msg_get_payload_size, nopoll_conn_send_text, nopoll_loop_wait