From a0363723353a7c41901357d48e22de08690b409d Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Fri, 4 Dec 2020 23:40:47 +0100 Subject: [PATCH] some websocket shenanigans --- .gitignore | 2 ++ backend/resources/public/index.html | 12 +++++++++++- frontend/src/Main.elm | 18 ++++++++++++++++-- frontend/src/Model.elm | 6 +++++- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index d3977dc..de6c4c0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ /frontend/elm-stuff/ /frontend/index.html /frontend/compiled/ +backend/.nrepl-port +.idea/sonarlint diff --git a/backend/resources/public/index.html b/backend/resources/public/index.html index c030aff..121ea5a 100644 --- a/backend/resources/public/index.html +++ b/backend/resources/public/index.html @@ -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) + }) diff --git a/frontend/src/Main.elm b/frontend/src/Main.elm index b8086db..15a0db1 100644 --- a/frontend/src/Main.elm +++ b/frontend/src/Main.elm @@ -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 = diff --git a/frontend/src/Model.elm b/frontend/src/Model.elm index 8e1c4d2..37639aa 100644 --- a/frontend/src/Model.elm +++ b/frontend/src/Model.elm @@ -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 }