freddie v0.1.3 Freddie.Router

Freddie.Router is a module for protocol handling.

This module dispatch messages from socket. You can use meta of packet and msg and context.

  • meta => Freddie.Scheme.Common.Meta

  • msg => Custom Packet

  • context => Freddie.Context

Examples

defmodule EchoServer.Router do
  use Freddie.Router

  require Logger

  # defined pakcet schemes
  alias EchoServer.Scheme

  # defined event handlers
  alias EchoServer.Handler

  defhandler EchoServer.Scheme.CS_Echo do
    echo = Scheme.SC_Echo.new(msg: msg.msg)
    Freddie.Session.send(context, echo)
  end

  defhandler EchoServer.Scheme.CS_EncryptPing do
    case meta.use_encryption do
      true ->
        # do process
      false ->
        # do other process
    end
    pong = Scheme.SC_EncryptPong.new(msg: "Pong!", idx: msg.idx + 1)
    Freddie.Session.send(context, pong, use_encryption: true)
  end

  defhandler EchoServer.Scheme.CS_Login do
    {context, msg}
    |> Handler.Login.handle()
  end

  # connect is a special handler who can control the connection of a session.
  connect do
    Logger.info("Client is connected!")
  end

  # disconnect is a special handler who can control the disconnection of a session.
  disconnect do
    Logger.info("Client is disconnected!")
  end

end

Link to this section Summary

Link to this section Functions

Link to this macro

connect(body) (macro)

Link to this macro

default(body) (macro)

Link to this macro

disconnect(body) (macro)

Link to this function

get_scheme_seq(scheme)

Link to this function

load_scheme_table()