@@ -29,6 +29,7 @@ instance Reflex t => Default (WebSocketConfig t) where
30data WebSocket t
30data WebSocket t
31 = WebSocket { _webSocket_recv :: Event t ByteString
31 = WebSocket { _webSocket_recv :: Event t ByteString
32+ , _webSocket_open :: Event t ()
34webSocket :: forall t m. (HasWebView m, MonadWidget t m) => String -> WebSocketConfig t -> m (WebSocket t)
35webSocket :: forall t m. (HasWebView m, MonadWidget t m) => String -> WebSocketConfig t -> m (WebSocket t)
@@ -39,12 +40,16 @@ webSocket url config = do
39 (eRecv, eRecvTriggerRef) <- newEventWithTriggerRef
40 (eRecv, eRecvTriggerRef) <- newEventWithTriggerRef
40 currentSocketRef <- liftIO $ newIORef Nothing
41 currentSocketRef <- liftIO $ newIORef Nothing
41 --TODO: Disconnect if value no longer needed
42 --TODO: Disconnect if value no longer needed
43+ (eOpen, eOpenTriggerRef) <- newEventWithTriggerRef
42 let onMessage :: ByteString -> IO ()
44 let onMessage :: ByteString -> IO ()
43 onMessage m = postGui $ do
45 onMessage m = postGui $ do
44 mt <- readRef eRecvTriggerRef
46 mt <- readRef eRecvTriggerRef
45 forM_ mt $ \t -> runWithActions [t :=> m]
47 forM_ mt $ \t -> runWithActions [t :=> m]
48+ onOpen = postGui $ do
49+ mt <- readRef eOpenTriggerRef
50+ forM_ mt $ \t -> runWithActions [t :=> ()]
46 start = do
51 start = do
47− ws <- liftIO $ newWebSocket wv url onMessage $ do
52+ ws <- liftIO $ newWebSocket wv url onMessage onOpen $ do
48 void $ forkIO $ do --TODO: Is the fork necessary, or do event handlers run in their own threads automatically?
53 void $ forkIO $ do --TODO: Is the fork necessary, or do event handlers run in their own threads automatically?
49 liftIO $ writeIORef currentSocketRef Nothing
54 liftIO $ writeIORef currentSocketRef Nothing
50 liftIO $ threadDelay 1000000
55 liftIO $ threadDelay 1000000
@@ -58,6 +63,6 @@ webSocket url config = do
58 Nothing -> return () -- Discard --TODO: should we do something better here? probably buffer it, since we handle reconnection logic; how do we verify that the server has received things?
63 Nothing -> return () -- Discard --TODO: should we do something better here? probably buffer it, since we handle reconnection logic; how do we verify that the server has received things?
59 Just ws -> do
64 Just ws -> do
60 liftIO $ webSocketSend ws payload
65 liftIO $ webSocketSend ws payload
61− return $ WebSocket eRecv
66+ return $ WebSocket eRecv eOpen
63makeLensesWith (lensRules & simpleLenses .~ True) ''WebSocketConfig
68makeLensesWith (lensRules & simpleLenses .~ True) ''WebSocketConfig