billstclair/elm-websocket-framework-server/2.0.0

NOTE: this package is not compatible with Elm 0.19.1

WebSocketFramework.Server

Support for a Node.js server for WebSocketFramework messages.

Types

type alias Model servermodel message gamestate player =
{ servermodel : servermodel
, userFunctions :
UserFunctions servermodel message gamestate player
, state : ServerState gamestate player
, gameidDict : Dict Socket GameId
, playeridDict : Dict GameId (List PlayerId)
, socketsDict : Dict GameId (List Socket)
, deathWatch : List DeathWatch
, deathWatchGameids : DeathWatchGameids
, time : Time
, seed : Seed
}

The server application model.

program creates one of these, via a call to init.

type Msg
= Connection Socket
| Disconnection Socket
| SocketMessage Socket String
| FirstTick Time
| Tick Time
| Noop

The messages processed by our update function.

Connection, Disconnection, and SocketMessage from through the inputPort from the Node.JS code. FirstTick and Tick are used to track time. Noop does nothing.

type alias ServerMessageSender servermodel message gamestate player =
WrappedModel servermodel message gamestate player
-> Socket
-> ServerState gamestate player
-> message
-> message
-> ( WrappedModel servermodel message gamestate player, Cmd Msg )

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

This will usually call sentToOne 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.

type alias UserFunctions servermodel message gamestate player =
{ encodeDecode : EncodeDecode message
, messageProcessor :
ServerMessageProcessor gamestate player message
, messageSender :
ServerMessageSender servermodel message gamestate player
, messageToGameid : Maybe (MessageToGameid message)
, inputPort : InputPort Msg
, outputPort : 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.

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

type WrappedModel servermodel message gamestate player
= WrappedModel (Model servermodel message gamestate player)

A type wrapper to prevent recursive types in Model.

type alias Socket =
Socket

An alias of WebSocketServer.Socket.

Top-level program

servermodel
-> UserFunctions servermodel message gamestate player
-> Maybe gamestate
-> Program Never (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.

servermodel
-> UserFunctions servermodel message gamestate player
-> Maybe gamestate
-> ( Model servermodel message gamestate player, Cmd Msg )

Return the initial Model and a Cmd to get the current time.

Usually called for you by program.

Message sending

MessageEncoder message
-> message
-> OutputPort Msg
-> Socket
-> Cmd Msg

Send a message to a single socket.

MessageEncoder message
-> message
-> OutputPort Msg
-> List Socket
-> Cmd Msg

Send a message to a multiple sockets.

Support for creating random game and player identifiers.

WrappedModel servermodel message gamestate player
-> ( GameId, WrappedModel servermodel message gamestate player )

Generate a random GameId string, ensuring that it is not already assigned to a game.

Will not be used by servers that have no concept of a game.

WrappedModel servermodel message gamestate player
-> ( PlayerId, WrappedModel servermodel message gamestate player )

Generate a random PlayerId string, ensuring that it is not already assigned to a player.

Will not be used by servers that have no concept of a player.