Commit 4600b43e authored by Cédric F.'s avatar Cédric F.

Hook up with web socket

parent 9320c2b2
......@@ -15,6 +15,7 @@
"elm-lang/keyboard": "1.0.1 <= v < 2.0.0",
"elm-lang/navigation": "2.1.0 <= v < 3.0.0",
"elm-lang/virtual-dom": "2.0.4 <= v < 3.0.0",
"elm-lang/websocket": "1.0.2 <= v < 2.0.0",
"elm-lang/window": "1.0.1 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
......
......@@ -22,6 +22,11 @@ http {
limit_req_zone $binary_remote_addr zone=default:10m rate=5r/s;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 8000 default_server ssl http2;
server_name _;
......@@ -54,6 +59,8 @@ http {
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
limit_req zone=default burst=10;
error_page 502 @maintenance;
root public;
......@@ -73,9 +80,7 @@ http {
}
location = /rpc {
limit_req zone=default burst=10;
proxy_set_header Authorization "Basic SWp4enNybXNybHFoZGRna2ZqNTVmZ2tjZU1jc3Rzb2F6aWthc2VzdWM6c3RhMmJqYmZ5cWN0eXRxdWpvbGNhdGJ2dmtzanVsclVxcnpjNnd1TWM=";
proxy_set_header Authorization "Basic Yml0Y29pbjpzZWNyZXQ="; # bitcoin:secret
proxy_set_header Host $host;
proxy_cache rpc;
......@@ -92,6 +97,21 @@ http {
# proxy_pass http://localhost:8332/; # bitcoind
}
location /ws {
#proxy_ssl_ciphers HIGH:!aNULL:!MD5;
#proxy_ssl_session_reuse on;
proxy_http_version 1.1;
# proxy_read_timeout 86400;
proxy_set_header Authorization "Basic Yml0Y29pbjpzZWNyZXQ="; # bitcoin:secret
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass https://localhost:9109/ws;
}
location / {
try_files $uri /index.html;
}
......
......@@ -14,6 +14,7 @@ type alias Model =
, connections : Int
, fetching : Bool
, error : Maybe String
, webSocketConnected : Bool
}
......@@ -23,6 +24,7 @@ initialModel =
, connections = -1
, fetching = False
, error = Nothing
, webSocketConnected = False
}
......@@ -45,6 +47,12 @@ view model =
span [] [ text "??????" ]
else
queryLink (toString model.blocks) (toString model.blocks) []
( wsClass, wsStatus ) =
if model.webSocketConnected then
( "success", "on" )
else
( "danger", "off" )
in
div []
[ span
......@@ -57,8 +65,8 @@ view model =
]
]
, span
[ class "badge badge-pill badge-danger" ]
[ text "Live updating:<br>websocket disconnected" ]
[ class <| "badge badge-pill badge-" ++ wsClass ]
[ text <| "Live updating: " ++ wsStatus ]
]
......
module Lib.WebSocket exposing (send, isSuccess)
import WebSocket
import Json.Encode
send : String -> String -> List Json.Encode.Value -> Cmd msg
send endpoint method params =
let
json =
Json.Encode.object
[ ( "jsonrpc", Json.Encode.string "2.0" )
, ( "id", Json.Encode.int 0 )
, ( "method", Json.Encode.string method )
, ( "params", Json.Encode.list params )
]
in
json
|> Json.Encode.encode 0
|> WebSocket.send endpoint
isSuccess : String -> Bool
isSuccess jsonString =
jsonString == "{\"result\":null,\"error\":null,\"id\":0}"
-- decode : String -> Json.Decode.Decoder Notification
-- decode jsonString =
-- Json.Decode.map2 JsonModel
-- (Json.Decode.at [ "result", "blocks" ] Json.Decode.int)
-- (Json.Decode.at [ "result", "connections" ] Json.Decode.int)
......@@ -37,14 +37,15 @@ type alias Model =
, query : String
, template : Template
, error : Maybe String
, wsEndpoint : String
, vimMode : Bool
, debug : Bool
, time : Time
}
initialModel : Coin -> String -> Model
initialModel coin query =
initialModel : Coin -> String -> String -> Model
initialModel coin wsEndpoint query =
{ config = Config coin
, keys = Keys False False False False False False
, window = Window.Size 0 0
......@@ -55,6 +56,7 @@ initialModel coin query =
, query = query
, template = Status
, error = Nothing
, wsEndpoint = wsEndpoint
, vimMode = False
, debug = False
, time = 0
......@@ -72,6 +74,7 @@ type Msg
| KeyChange Bool Keyboard.KeyCode
| Resize Window.Size
| Tick Time
| WSMsg String
| FetchStatus
......
......@@ -5,11 +5,13 @@ import Keyboard
import Task exposing (Task)
import Time exposing (Time)
import Window
import Trappisto.Model exposing (..)
import WebSocket
import Components.Status as StatusComponent
import Components.Address as AddressComponent
import Components.Block as BlockComponent
import Components.Transaction as TransactionComponent
import Lib.WebSocket
import Trappisto.Model exposing (..)
import Trappisto.Helpers as Coin exposing (Coin)
......@@ -25,6 +27,9 @@ init flags location =
query =
extractQuery location
wsEndpoint =
extractWSEndpoint location
coin =
case flags.coin of
"BCH" ->
......@@ -43,30 +48,39 @@ init flags location =
++ " (valid coins are BCH, BTC and DCR)"
model =
initialModel coin query
initialModel coin wsEndpoint query
( updatedModel, msg ) =
if String.isEmpty query then
update FetchStatus model
else
update (Query query) model
notifyBlocks =
Lib.WebSocket.send model.wsEndpoint "notifyblocks" []
notifyNewTransactions =
Lib.WebSocket.send model.wsEndpoint "notifynewtransactions" []
in
( updatedModel
, Cmd.batch
[ elmToJs [ "focus" ]
, msg
, notifyBlocks
, notifyNewTransactions
, Task.perform Resize Window.size
]
)
subscriptions : Model -> Sub Msg
subscriptions _ =
subscriptions model =
Sub.batch
[ Keyboard.downs (KeyChange True)
, Keyboard.ups (KeyChange False)
, Window.resizes Resize
, Time.every (Time.second * 60) Tick
, WebSocket.listen model.wsEndpoint WSMsg
, jsToElm JsMsg
]
......@@ -75,13 +89,30 @@ update : Msg -> Model -> ( Model, Cmd Msg )
update action model =
case action of
Tick time ->
-- let
-- -- TODO: ideally we should be able to trigger all updates when
-- -- we receive a websocket event and not use polling altogether
-- -- ( updatedModel, cmd ) =
-- -- update (Query model.query) model
-- in
-- ( { model | time = time }, notifyBlocks )
( model, Cmd.none )
WSMsg message ->
let
-- TODO: ideally we should be able to trigger all updates when
-- we receive a websocket event and not use polling altogether
( updatedModel, cmd ) =
update (Query model.query) model
_ =
Debug.log "WSMsg" message
connected =
Lib.WebSocket.isSuccess message
status =
model.statusModel
updatedStatus =
{ status | webSocketConnected = connected }
in
( { updatedModel | time = time }, cmd )
( { model | statusModel = updatedStatus }, Cmd.none )
NewUrl location ->
( { model | query = extractQuery location }, Cmd.none )
......@@ -336,6 +367,16 @@ extractQuery location =
pathname
extractWSEndpoint : Navigation.Location -> String
extractWSEndpoint location =
String.concat
[ "wss://"
, location.host
, location.pathname
, "ws"
]
newUrl : Model -> Cmd Msg
newUrl model =
Navigation.newUrl <| "/#" ++ model.query
......
......@@ -18,6 +18,7 @@
"elm-lang/keyboard": "1.0.1 <= v < 2.0.0",
"elm-lang/navigation": "2.1.0 <= v < 3.0.0",
"elm-lang/virtual-dom": "2.0.4 <= v < 3.0.0",
"elm-lang/websocket": "1.0.2 <= v < 2.0.0",
"elm-lang/window": "1.0.1 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
......
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