Skip to content

Instantly share code, notes, and snippets.

@knowthen
Last active May 6, 2016 16:15
Show Gist options
  • Save knowthen/9348d4eda390139a82de91dc75167eab to your computer and use it in GitHub Desktop.
Save knowthen/9348d4eda390139a82de91dc75167eab to your computer and use it in GitHub Desktop.
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