This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New sample : Websocket + Auto refresh + Hot Module replacement
- Loading branch information
Maxime Mangel
committed
Aug 21, 2016
1 parent
bc65e2b
commit abe4328
Showing
6 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#r "../node_modules/fable-core/Fable.Core.dll" | ||
#load "../node_modules/fable-import-virtualdom/Fable.Helpers.Virtualdom.fs" | ||
open Fable.Core | ||
open Fable.Core.JsInterop | ||
open Fable.Import | ||
open Fable.Import.Browser | ||
|
||
open Fable.Helpers.Virtualdom | ||
open Fable.Helpers.Virtualdom.App | ||
open Fable.Helpers.Virtualdom.Html | ||
|
||
|
||
open System | ||
|
||
type Model = | ||
{ Input: string | ||
Messages: string list} | ||
|
||
static member initial = | ||
{ Input = "" | ||
Messages = []} | ||
|
||
type Action = | ||
| NoOp | ||
| ChangeInput of string | ||
| SendEcho | ||
| ReceivedEcho of string | ||
|
||
let webSocket = | ||
WebSocket.Create("wss://echo.websocket.org") | ||
|
||
let update (model: Model) action = | ||
let model', action' = | ||
match action with | ||
| ReceivedEcho msg -> | ||
{ model with Messages = msg :: model.Messages } ,[] | ||
| ChangeInput s -> | ||
{ model with Input = s }, [] | ||
| SendEcho -> | ||
{ model with Input = "" }, [] | ||
| _ -> | ||
model, [] | ||
|
||
let jsCall = | ||
match action with | ||
| SendEcho -> webSocket.send(model.Input) | ||
| _ -> () | ||
|
||
model', action' | ||
|
||
let inline onInput x = onEvent "oninput" (fun e -> x (unbox e?target?value)) | ||
let onEnter succ nop = onKeyup (fun x -> if (unbox x?keyCode) = 13 then succ else nop) | ||
|
||
let viewMessage msg = | ||
div | ||
[] | ||
[ span | ||
[] | ||
[ text msg ] | ||
br [] | ||
] | ||
|
||
let view model = | ||
div | ||
[] | ||
[ div | ||
[] | ||
[ text "Guide" | ||
ol | ||
[] | ||
[ li | ||
[] | ||
[ text "Enter your text" ] | ||
li | ||
[] | ||
[ text "Press enter or click the button" ] | ||
li | ||
[] | ||
[ text "See the message been received and added to the list" ] | ||
] | ||
] | ||
input | ||
[ onInput (fun x -> ChangeInput x) | ||
property "value" model.Input | ||
onEnter SendEcho NoOp ] | ||
button | ||
[ onMouseClick (fun _ -> SendEcho) ] | ||
[ text "Send" ] | ||
br [] | ||
span | ||
[] | ||
[ text "Messages received:" ] | ||
div | ||
[] | ||
(model.Messages |> List.map(fun x -> viewMessage x)) | ||
] | ||
|
||
let webSocketProducer push = | ||
webSocket.addEventListener_message( | ||
Func<_,_>(fun e -> | ||
push(ReceivedEcho (unbox e.data)) | ||
null | ||
) | ||
) | ||
|
||
createApp Model.initial view update | ||
|> withProducer webSocketProducer | ||
|> start renderer |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"module": "amd", | ||
"sourceMaps": true, | ||
"projFile": "./Source/Main.fsx", | ||
"outDir": "out" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="fr-FR"> | ||
<head> | ||
<meta charSet="utf-8"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>BridgeStream</title> | ||
<meta name="description" content="Application to make draw from Twitch and Youtube live tchat"/> | ||
<meta name="author" content="Mangel Maxime"/> | ||
</head> | ||
<body> | ||
<script src="/public/bundle.js"></script> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "browser", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "node_modules/.bin/fable", | ||
"watch": "npm run build -- -w", | ||
"dev": "node_modules/.bin/concurrently 'npm run watch' 'npm run wp-server'", | ||
"wp-server": "node_modules/.bin/webpack-dev-server --hot" | ||
}, | ||
"author": "Maxime Mangel <mangel.maxime@outlook.com>", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"concurrently": "^2.2.0", | ||
"fable-compiler": "^0.5.6", | ||
"fable-import-virtualdom": "^0.6.7", | ||
"html-webpack-plugin": "^2.22.0", | ||
"source-map-loader": "^0.1.5", | ||
"webpack": "^1.13.2", | ||
"webpack-dev-server": "^1.14.1" | ||
}, | ||
"dependencies": { | ||
"core-js": "^2.4.1", | ||
"fable-core": "^0.5.4", | ||
"virtual-dom": "^2.1.1" | ||
}, | ||
"engines": { | ||
"fable": "^0.5.6" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var path = require("path"); | ||
var webpack = require("webpack"); | ||
|
||
var cfg = { | ||
devtool: "source-map", | ||
entry: "./out/Main.js", | ||
output: { | ||
path: path.join(__dirname, "public"), | ||
publicPath: "/public/", | ||
filename: "bundle.js" | ||
}, | ||
module: { | ||
preLoaders: [ | ||
{ | ||
test: /\.js$/, | ||
exclude: /node_modules/, | ||
loader: "source-map-loader", | ||
include: "path.join(__dirname, 'public')" | ||
} | ||
] | ||
}, | ||
devServer: { | ||
contentBase: "out/", | ||
inline: true, | ||
publicPath: "/public/" | ||
} | ||
}; | ||
|
||
module.exports = cfg; |