Expand/Shrink

nopoll_conn_set_on_msg

Definition: include nopoll.e

nopoll_conn_set_on_msg(atom conn, on_msg_cb, pUserData=NULL)
Description: Configure an on message handler on the provided connection that overrides the one configured at noPollCtx.

conn: The connection to be configured with a particular on message handler.
on_msg_cb: The on message handler configured.
pUserData: User defined pointer to be passed to the on message handler when it is called.
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?? conn = ??
-- 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_conn_set_on_msg(conn, 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_ctx_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