some websocket shenanigans

master
Josha von Gizycki 4 years ago
parent b8f07a7b34
commit a036372335

2
.gitignore vendored

@ -5,3 +5,5 @@
/frontend/elm-stuff/
/frontend/index.html
/frontend/compiled/
backend/.nrepl-port
.idea/sonarlint

@ -27,11 +27,21 @@
console.debug("your uuid is " + uuid)
Elm.Main.init({
const app = Elm.Main.init({
flags: {
userUuid: uuid
}
})
const ws = new WebSocket(
"ws://" + window.location.hostname + ":" + window.location.port + "/ws")
ws.onmessage = function(event) {
console.debug(event)
app.ports.wsin.send(JSON.stringify(event.data))
}
app.ports.wsout.subscribe(function(msg) {
ws.send(msg)
})
</script>
</body>
</html>

@ -1,4 +1,4 @@
module Main exposing (main)
port module Main exposing (main)
import Browser
import Browser.Navigation as Nav
@ -21,6 +21,12 @@ main =
}
port wsout : String -> Cmd msg
port wsin : (String -> msg) -> Sub msg
{-| currently no flags are needed
that's the reason for the generic type and \_ as param name
-}
@ -45,12 +51,14 @@ init flags url key =
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none
wsin WsIn
type Msg
= UrlChanged Url.Url
| LinkClicked Browser.UrlRequest
| WsIn String
| WsOut String
update : Msg -> Model -> ( Model, Cmd Msg )
@ -69,6 +77,12 @@ update msg model =
-- TODO error reporting
( { model | route = Navigation.Home }, Cmd.none )
WsOut wsmsg ->
( model, wsout wsmsg )
WsIn wsmsg ->
Debug.log wsmsg ( model, Cmd.none )
routeChanged : Navigation.Route -> Model -> ( Model, Cmd Msg )
routeChanged route model =

@ -6,12 +6,16 @@ import Navigation
import Url exposing (Url)
type alias Uuid =
String
type alias Model =
{ key : Nav.Key
, apiHost : String
, route : Navigation.Route
, httpError : Maybe Http.Error
, userUuid : String
, userUuid : Uuid
}

Loading…
Cancel
Save