billstclair / elm-websocket-framework-server / WebSocketFramework.Server

Support for a Node.js server for WebSocketFramework messages.

Types


type Model servermodel message gamestate player

An opaque type containing the application state.


type alias Socket =
WebSocketServer.Socket

An alias of WebSocketServer.Socket.


type Msg

The messages processed by our update function.

Opaque, because your code doesn't need to know.

Callbacks


type alias UserFunctions servermodel message gamestate player =
{ encodeDecode : WebSocketFramework.Types.EncodeDecode message
, messageProcessor : WebSocketFramework.Types.ServerMessageProcessor gamestate player message
, messageSender : ServerMessageSender servermodel message gamestate player
, messageToGameid : Maybe (message -> Maybe WebSocketFramework.Types.GameId)
, messageToPlayerid : Maybe (message -> Maybe WebSocketFramework.Types.PlayerId)
, autoDeleteGame : Maybe (WebSocketFramework.Types.GameId -> WebSocketFramework.Types.ServerState gamestate player -> Basics.Bool)
, gamesDeleter : Maybe (ServerGamesDeleter servermodel message gamestate player)
, playersDeleter : Maybe (ServerPlayersDeleter servermodel message gamestate player)
, inputPort : WebSocketFramework.Types.InputPort Msg
, outputPort : WebSocketFramework.Types.OutputPort Msg 
}

User functions that get called by the generic server code.

encodeDecode is used to translate messages to and from strings.

messageProcessor processes a client request into state changes and a response message.

messageSender decides what to do with the response message.

messageToGameid extracts a GameId from a message, if there is one.

messageToPlayerid extracts a PlayerId from a message, if there is one.

autoDeleteGame is called when all sockets referencing a GameId have disconnected. If it returns True, then the game will be put on deathwatch, meaning it will be removed from the tables after a delay. Usually used to keep public games from being auto-deleted.

gamesDeleter is called when games are deleted due to their sockets being disconnected. See the ServerGamesDeleter description for more details.

playersDeleter is called when players are deleted due to their sockets being disconnected. See the ServerPlayersDeleter description for more details.

inputPort and outputPort are the ports used to communicate with the Node.js code.


type alias ServerMessageSender servermodel message gamestate player =
Model servermodel message gamestate player -> Socket -> WebSocketFramework.Types.ServerState gamestate player -> message -> message -> ( Model servermodel message gamestate player
, Platform.Cmd.Cmd Msg 
}

User function that is called to send the response(s) to a request.

This will usually call sendToOne and/or sendToMany with the message emitted by the ServiceMessageProcessor in the UserFunctions passed to program.

The first message is the request that came from client to server. The second message is the response. If no response is returned by the ServiceMessageProcessor, this function is not called.

The ServerState arg is the value of the state property of the Model arg, pulled out for your convenience. If you change it, you must put it back in the model you return.


type alias ServerGamesDeleter servermodel message gamestate player =
Model servermodel message gamestate player -> List WebSocketFramework.Types.GameId -> WebSocketFramework.Types.ServerState gamestate player -> ( Model servermodel message gamestate player
, Platform.Cmd.Cmd Msg 
}

Called when games are auto-deleted due to socket connections being lost.

This will only happen if your server code tracks the association between sockets, games and players in the xxxDict properties of the Model. When tracked games are dropped, this function, stored in the gamesDeleter property of UserFunctions, is called, so that you can clean up any reference to those games in your gamestate.

It is called BEFORE the game information is removed from the ServerState.


type alias ServerPlayersDeleter servermodel message gamestate player =
Model servermodel message gamestate player -> WebSocketFramework.Types.GameId -> List WebSocketFramework.Types.PlayerId -> WebSocketFramework.Types.ServerState gamestate player -> ( Model servermodel message gamestate player
, Platform.Cmd.Cmd Msg 
}

Called when players are auto-deleted due to socket connections being lost.

This will only happen if your server code tracks the association between sockets, games and players in the xxxDict properties of the Model. When tracked players are dropped, this function, stored in the playersDelete property of UserFunctions, is called, so that you can clean up any reference to those players in your gamestate.

It is called BEFORE the player information is removed from the ServerState.

Top-level program

program : servermodel -> UserFunctions servermodel message gamestate player -> Maybe gamestate -> Platform.Program (Maybe String) (Model servermodel message gamestate player) Msg

Create the top-level application program.

You will usually use the result of this function as the value of main in your top-level module.

Most servers will not need to use the servermodel, but it's a place to stash extra server-wide state that doesn't make sense in the game-specific gamestate, which is stored in the ServerModel, accessible via getServerModel.

Message sending

sendToOne : WebSocketFramework.Types.MessageEncoder message -> message -> WebSocketFramework.Types.OutputPort Msg -> Socket -> Platform.Cmd.Cmd Msg

Encode a message to a single socket via an output port.

If the first arg is True, log the operation on the console.

sendToMany : WebSocketFramework.Types.MessageEncoder message -> message -> WebSocketFramework.Types.OutputPort Msg -> List Socket -> Platform.Cmd.Cmd Msg

Encode a message to multiple sockets via an output port.

sendToOthers : WebSocketFramework.Types.GameId -> Socket -> Model servermodel message gamestate player -> WebSocketFramework.Types.MessageEncoder message -> message -> Platform.Cmd.Cmd Msg

Encode a message to all the sockets for a GameId except the passed one.

sendToAll : WebSocketFramework.Types.GameId -> Model servermodel message gamestate player -> WebSocketFramework.Types.MessageEncoder message -> message -> Platform.Cmd.Cmd Msg

Encode a message to all the sockets for a GameId.

State accessors

getState : Model servermodel message gamestate player -> WebSocketFramework.Types.ServerState gamestate player

Get the servermodel from a model.

setState : Model servermodel message gamestate player -> WebSocketFramework.Types.ServerState gamestate player -> Model servermodel message gamestate player

Set the ServerState in a model.

getServerModel : Model servermodel message gamestate player -> servermodel

Get the servermodel from a model.

setServerModel : Model servermodel message gamestate player -> servermodel -> Model servermodel message gamestate player

Set the servermodel in a model.

getDeathRowDuration : Model servermodel message gamestate player -> Basics.Int

Get the death row duration from a model.

This is the time a game or player sticks around after no connections reference it.

setDeathRowDuration : Model servermodel message gamestate player -> Basics.Int -> Model servermodel message gamestate player

Set the death row duration in a model.

This is the time a game or player sticks around after no connections reference it.

getTime : Model servermodel message gamestate player -> Basics.Int

Get the current time from a model.

The time is updated once a second.

Utilities

otherSockets : WebSocketFramework.Types.GameId -> Socket -> Model servermodel message gamestate player -> List Socket

Return sockets associated with a game.

Removes the passed socket from the list.

Often useful in ServerMessageSender functions to send responses to all players.

Model accessors

verbose : Model servermodel message gamestate player -> Basics.Bool

Return whether VERBOSE is set in the server's environment