Created
March 15, 2014 23:46
-
-
Save bohde/9575682 to your computer and use it in GitHub Desktop.
Use websockets with Control.Reactive in Purescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.Eff | |
import Control.Reactive | |
import Debug.Trace | |
import Prelude | |
foreign import websocket | |
"function websocket(url) {\ | |
\ return function(outgoing) {\ | |
\ return function(){\ | |
\ var conn = new WebSocket(url);\ | |
\ var buff = [];\ | |
\ var incoming = _ps.Control_Reactive.newRVar(\"\")();\ | |
\ conn.onopen = function(e) {\ | |
\ buff.forEach(function(msg){conn.send(msg)});\ | |
\ buff=[];\ | |
\ };\ | |
\ outgoing.subscribe(function(msg){\ | |
\ conn.readyState === \"OPEN\" ? conn.send(msg) : buff.push(msg);\ | |
\ });\ | |
\ conn.onmessage = function(e){\ | |
\ incoming.update(e.data);\ | |
\ };\ | |
\ return incoming;\ | |
\ };\ | |
\ };\ | |
\}" :: forall eff. String -> RVar String -> Eff (reactive :: Reactive | eff) (RVar String) | |
currentTrack = "{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"core.playback.get_current_track\"}" | |
main = do | |
outgoing <- newRVar "" | |
incoming <- websocket "ws://192.168.1.129:6680/mopidy/ws/" outgoing | |
subscribe incoming trace | |
writeRVar outgoing currentTrack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment