Last active
August 29, 2015 14:05
-
-
Save mk2/08b8decaf22d74b9061e to your computer and use it in GitHub Desktop.
ElmでWebSocket
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 Graphics.Input (Input, input, button) | |
import Graphics.Input.Field (Content, noContent, field, defaultStyle) | |
import Dict | |
import Text (centered, toText) | |
import WebSocket | |
import Debug (log) | |
{-- | |
ElmでWebsocket | |
@author mk2 | |
--} | |
-- リクエストURL | |
url = "ws://echo.websocket.org" | |
{-------------- | |
ユーザー入力周り | |
--------------} | |
-- サンプルテキスト入力フィールド | |
msg : Input Content | |
msg = input noContent | |
msgField : Content -> Element | |
msgField = field defaultStyle msg.handle id "Msg" | |
-- レスポンスを表示する関数 | |
dispResponse : String -> Element | |
dispResponse response = | |
let toElem = centered . toText | |
in toElem <| "" ++ response | |
contentString content = content.string | |
-- レスポンスを示すシグナル | |
responseSignal : Signal String | |
responseSignal = WebSocket.connect url <| contentString <~ msg.signal | |
-- 入力フォームを表示するための関数 | |
msgForm : Content -> Element | |
msgForm msgContent = | |
flow right [ | |
msgField msgContent | |
] | |
-- 全体(入力フォーム、送信ボタン)を表示するための関数 | |
display msgContent response = | |
flow down [ | |
msgForm msgContent | |
, dispResponse response | |
] | |
main = display <~ msg.signal ~ responseSignal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment