billstclair / elm-websocket-framework / WebSocketFramework.Types

Types used by the rest of the WebSocketFramework modules.

State


type alias ServerState gamestate player =
{ dicts : Dicts gamestate player
, publicGames : PublicGames
, state : Maybe gamestate
, seed : Random.Seed
, changes : Maybe Changes
, statistics : Maybe Statistics
, time : Time.Posix 
}

The part of the server state that is independent from its socket connections.

To access the opaque dicts, use addGame, addPlayer, getGame, getPlayer, getGamePlayers, updateGame, updatePlayer, removeGame, removePlayer from WebsocketFramework.ServerInterface.


type alias Dicts gamestate player =
WebSocketFramework.InternalTypes.DictsWrapper (WebSocketFramework.InternalTypes.ServerDicts gamestate player)

Opaque type used to store game and player state.


type ServerInterface gamestate player message msg
    = ServerInterface ({ server : ServerUrl, serverPort : Json.Encode.Value -> Platform.Cmd.Cmd msg, wrapper : ServerInterface gamestate player message msg -> message -> msg, state : Maybe (ServerState gamestate player), sender : ServerInterface gamestate player message msg -> message -> Platform.Cmd.Cmd msg })

Everything necessary to communicate with a server, be it real or proxy.


type alias PlayerInfo player =
{ gameid : GameId
, player : player 
}

Information about a player in a game.


type alias PublicGame =
{ gameid : GameId
, playerName : String 
}

If your server supports public games, this represents the game id and name of the creator of the game.


type alias PublicGames =
List PublicGame

A list of pubic games.


type alias Changes =
{ addedGames : List GameId
, addedPlayers : List ( GameId
, PlayerId )
, removedGames : List ( GameId
, List PlayerId )
, removedPlayers : List ( GameId
, PlayerId ) 
}

Used to inform the server of added and removed games and players.

Interact with it via ServerInterface.addGame, addPlayer, removeGame, removePlayer.

Empty states

emptyServerState : Maybe gamestate -> ServerState gamestate player

Create a mostly empty ServerState.

You'll need to initialize the seed property in your real server, if you use newGameid and newPlayerid in WebSocketFramework.ServerInterface.

You'll need to initialize the statistics property, if you want to track statistics.

emptyPublicGames : PublicGames

An empty list of public games.

Messages


type alias RawMessage =
{ reqrsp : String
, msg : String
, plist : Plist 
}

A raw request, ready to be encoded to go over the wire.

reqrsp is "req" or "rsp" for request or response.

msg is the name of the message.

plist is the parameters of the message.


type ReqRsp
    = Req String
    | Rsp String

A type safe representation of the reqrsp and msg fields of a RawMessage.


type alias Plist =
List ( String
, Json.Encode.Value 
}

A list of key/value pairs.


type alias ErrorRsp message =
{ request : message
, text : String 
}

An error response that encapsulates a request message and an error string.

Function Signatures


type alias ServerMessageProcessor gamestate player message =
ServerState gamestate player -> message -> ( ServerState gamestate player
, Maybe message 
}

Type signature of a function which turns a server state and a request message into a ServerState and maybe a response message.


type alias MessageToGameid message =
message -> Maybe GameId

Type signature of a function that extracts the game id from a message, if it has one.


type alias ModeChecker gamestate message =
gamestate -> message -> Result String Basics.Never

Type signature of a function that checks if a message is a legal request for a gamestate.

An Err result has a message string. An Ok result maps an unusable value.

Message Encoding/Decoding


type alias MessageDecoder message =
( ReqRsp
, Plist ) -> Result String messag
)

Type signature for a function that turns a request or response and a key/value list into a message.


type alias MessageEncoder message =
message -> ( ReqRsp
, Plist 
}

Type signature for a function that turns a message into request or response and a key/value list.


type alias EncodeDecode message =
{ encoder : MessageEncoder message
, decoder : MessageDecoder message
, errorWrapper : Maybe (Error message -> message) 
}

Wrapping of encoder, decoder, and error message creator.


type alias DecoderPlist msg =
List ( String
, Json.Decode.Decoder msg 
}

Map a list of request or response names to decoders for their messages.

Statistics


type alias Statistics =
Dict String Basics.Int

Track statistics about connections and games.

emptyStatistics : Statistics

Return a new Statistics record, with your value for the statistics field.

Until you populate the ServerState.statistics field, no connection statistics will be gathered.

Utility

printifyString : String -> String

Convert ASCII double-quote characters to curly end quotes.

Server Ports


type alias InputPort msg =
(Json.Encode.Value -> msg) -> Platform.Sub.Sub msg

The input port from the Node.js code to the WebSocket server.


type alias OutputPort msg =
Json.Encode.Value -> Platform.Cmd.Cmd msg

The output port from the WebSocket server to the Node.js code.

Errors


type ErrorKind
    = JsonParseError
    | UnspecifiedError

The kind of error being reported in an Error instance.


type alias Error message =
{ kind : ErrorKind
, description : String
, message : Result String message 
}

Description of an error passed to EncodeDecode.errorWrapper.

If kind is JsonParseError, then description is the message text, and message is the error returned from parsing it.

If kind is UnspecifiedError, then description is user-level text, and message is Ok wrapped around the relevant message, if there is one, or Err wrapped around some likely useless string.

Aliases


type alias GameId =
WebSocketFramework.InternalTypes.GameId

An alias for String.


type alias PlayerId =
WebSocketFramework.InternalTypes.PlayerId

An alias for String.


type alias ServerUrl =
String

An alias for String.