-
-
Save knowthen/9348d4eda390139a82de91dc75167eab to your computer and use it in GitHub Desktop.
This file contains 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
import Html exposing (..) | |
import Html.App as Html | |
import Html.Events exposing (..) | |
import WebSocket exposing (..) | |
main : Program Never | |
main = | |
Html.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
-- MODEL | |
type alias Model = | |
{ msg : String | |
, error : String} | |
init : (Model, Cmd Msg) | |
init = | |
-- Subscriptions based Cmd's don't work here | |
(Model "" "", WebSocket.send url "hello first time" ) | |
-- UPDATE | |
type Msg | |
= Send String | |
| Receive String | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update action model = | |
case action of | |
Send msg -> | |
( model, WebSocket.send url msg ) | |
Receive msg -> | |
( { model | msg = msg }, Cmd.none ) | |
url : String | |
url = "ws://echo.websocket.org" | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = listen url Receive | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ button [ onClick (Send "Hello again") ] [ text "Say Hello" ] | |
, div [] [ text (model.msg) ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment