[websockets] correctly pick ws/wss protocol based on window.location

parent 557c4dc5
Pipeline #6258 failed with stages
......@@ -106,7 +106,8 @@ mainAppCpt = here.component "main" cpt where
(Sessions.Sessions { sessions }) <- T.read boxes.sessions
let session = Seq.head sessions
-- here.log2 "[mainApp] sessions" sessions'
Notifications.connect ws "ws://localhost:8008/ws" session
wsProto <- Notifications.wsProtocol
Notifications.connect ws (wsProto <> "://localhost:8008/ws") session
-- T.write_ ws boxes.wsNotification
-- NOTE: Dummy subscription
-- let action = NotificationsT.InsertCallback (NotificationsT.UpdateTree (-1)) "some-uuid" (\_ -> here.log "callback!")
......
......@@ -21,6 +21,7 @@ import Foreign as F
import Gargantext.Components.Notifications.Types
import Gargantext.Sessions.Types (Session(..))
import Gargantext.Types as GT
import Gargantext.Utils (protocol)
import Gargantext.Utils.Reactix as R2
import Prelude
import Reactix as R
......@@ -143,6 +144,17 @@ performAction (WSNotification ws') (Call notification) = do
callNotification state notification
-- | Correctly choose between "ws" and "wss" protocols based on what
-- | is the current window location
wsProtocol :: Effect String
wsProtocol = do
proto <- protocol
pure (if proto == "http" then "ws" else "wss")
-- | Main WebSockets connect functionality. Handles incoming messages,
-- | authorizes user (if logged in), tries to reconnect when the
-- | connection drops
connect :: WSNotification -> String -> (Maybe Session) -> Effect Unit
connect ws@(WSNotification ws') url session = do
mConn <- Ref.read ws'.connection
......
......@@ -17,7 +17,7 @@ import Partial.Unsafe (unsafePartial)
import Prelude
import Web.HTML as WHTML
import Web.HTML.Location as WHL
import Web.HTML.Window (location)
import Web.HTML.Window as WHW
-- | TODO (hard coded)
csrfMiddlewareToken :: String
......@@ -103,11 +103,14 @@ sortWith :: forall a b f. Functor f =>
sortWith f xs = A.toUnfoldable $ A.sortWith f (A.fromFoldable xs)
location :: Effect WHL.Location
location = WHTML.window >>= WHW.location
href :: Effect String
href = do
w <- WHTML.window
loc <- location w
WHL.href loc
href = location >>= WHL.href
protocol :: Effect String
protocol = location >>= WHL.protocol
nbsp :: Int -> String
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment