Hi, dear all…
sorry for my english,
i’m using Cowboy websocket part to handle client messages,
here is my WebSocket Handler code…
i start server by this…
def start( _type, _args ) do
dispatch = :cowboy_router.compile([
{ :_,
[
{ "/", Handler_WebSocket, [] },
]
}
])
startResult = :cowboy.start_clear( :server, 1000, [{ :port, 8080 }], %{:env => %{:dispatch => dispatch } } )
case startResult do
{ :ok, pid } ->
Logger.info "[ws] Server Started, ServicePort[#{ @servicePort }], Pid[#{inspect pid}]"
{ :ok, pid }
{ :error, errorCode } -> Logger.info "[ws] Server Error, #{ inspect errorCode }"
end
end
here is my Handler…
defmodule Handler_WebSocket do
@behaviour :cowboy_websocket
def init( req, state ) do
Logger.info "[ws.gate:init] get connection..."
{ :cowboy_websocket, req, cache, %{ :idle_timeout => LOH.Conn.timeoutOfIdle } }
end
def terminate( reason, _req, _state ) do
Logger.info "[ws.gate:terminate] disconnected, reason:[#{inspect reason}], client[#{inspect _state }]"
:ok
end
def websocket_handle({ :text, message } = _frame, state) do
Logger.info "[ws.gate] get client send[ #{ inspect _frame} ] c[#{ inspect self() }] state[#{inspect state}]"
# transfer to module
{ cache, replyMessage } = MyModule.handleWebSocketBy( state, message )
Logger.info "[ws.gate] send back to client[#{ state.ip }] message: [#{ replyMessage }]"
{ :reply, {:text, replyMessage}, cache }
end
def websocket_handle(_frame, state) do
Logger.info "[ws-websocket-handle-2] received Data[ #{ inspect _frame} ] c[#{ inspect self() }] state[#{inspect state}]"
end
def websocket_info({_timeout, _ref, _msg}, state) do
Logger.warn "[ws.gate] get message from websocket_info method, msg[#{inspect _msg}]"
end
def websocket_info(_info, state) do
Logger.warn "[ws.gate] websocket_info"
{:ok, state}
end
end
currently the websocket all perfect work,
i create another application to manage what’s message need to send to client,
i know i can store all connect client PID on websocket init, ( use like Cachex )
but when i want send message to all connected clients,
i have no idea how to do it…
anyone can tech me how to send message to client?
please help, thank you very much