Permalink
Browse files

chat example

  • Loading branch information...
1 parent 668ea56 commit 99365adfdd7a140e14c4715690bb576aa5dad538 @tsloughter tsloughter committed Nov 15, 2011
View
@@ -1,6 +1,6 @@
NAME = pubnub_test.exe
-SRC = pubnub.opp src/plugin/pubnub.opa src/examples/pubnub_chat.opa src/examples/pubnub_test.opa
+SRC = pubnub.opp src/plugin/pubnub.opa src/examples/pubnub_chat.opa
all : pubnub opa
@@ -1,93 +1,51 @@
-
import pubnub
-import stdlib.themes.bootstrap
-import stdlib.widgets.bootstrap
-
-
-action() =
- s = Dom.get_value(#input)
- username : RPC.Json.json = {String = "tristan"}
- message : RPC.Json.json = {String = s}
- record_json : RPC.Json.json = {Record = [("username", username), ("message", message)]}
- PubNub.publish("chat", record_json)
-
-button(id, message) =
- <a id={id:string}
- class="button"
- ref="#"
- onclick={_ -> action() }>{message:string}
- </a>
-
-add(x) =
+user_update(x) =
match Json.deserialize(x) with
- | {some={Record = [("username", username), ("message", message)]}} -> Dom.transform([ #box +<- Json.to_string(message) ])
- | {some=_} -> Dom.transform([ #box <- "fail" ])
- | {none} -> Dom.transform([ #box <- "none" ])
-
-page() =
- <>
- <div id=pubnub pub-key=demo sub-key=demo onready={_ -> PubNub.subscribe("chat", (x -> add(x))) }></div>
- <div><input id=input placeholder=you-chat-here /></div>
- <div id=box></div>
- {button("publish_client", "Send Message")}
- </>
-
-WB = WBootstrap
-
-common_unit =
- <div class="hero-unit">
- <h1>Hello, world!</h1>
- <p>Vestibulum id ligula porta felis euismod semper. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
- <p><a class="btn primary large">Learn more »</a></p>
- </div>
-
-common_row = WB.Grid.row([
- {span=6 offset=none content=
- <h2>Heading</h2>
- <p>Etiam porta sem malesuada magna mollis euismod. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
- <p><a class="btn" href="#">View details »</a></p>},
- {span=5 offset=none content=
- <h2>Heading</h2>
- <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
- <p><a class="btn" href="#">View details »</a></p>},
- {span=5 offset=none content=
- <h2>Heading</h2>
- <p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
- <p><a class="btn" href="#">View details »</a></p>}
-])
-
-common_footer =
- <footer>
- <p>© Company 2011</p>
- </footer>
-
-conatainer_app_content =
- WB.Div.content(
- WB.Div.page_header(1, "Page name", some("Supporting text or tagline")) <+>
- WB.Grid.row([
- {span=10 offset=none content=<div><h2>Chat</h2><ul id="box"></ul><div id=pubnub pub-key=demo sub-key=demo onready={_ -> PubNub.subscribe("chat", (x -> add(x))) }></div><div><input id=input placeholder=you-chat-here /></div>{button("publish_client", "Send Message")}</div>},
- {span=4 offset=none content=<h3>Users</h3>}
- ])
- ) <+>
- common_footer
-
-container_app() =
- WB.Navigation.topbar(
- WB.Layout.fixed(
- WB.Navigation.brand(<>Project name</>, some("/container-app"), ignore) <+>
- WB.Navigation.nav([
- {active=<>Home</> href=some("#") onclick=ignore},
- {inactive=<>About</> href=some("#about") onclick=ignore},
- {inactive=<>Contact</> href=some("#contact") onclick=ignore}
- ]) <+>
- (<form action="">
- <input class="input-small" type="text" placeholder="Username"/>
- <input class="input-small" type="password" placeholder="Password"/>
- <button class="btn" type="submit">Sign in</button>
- </form> |> WB.pull_right(_))
- )
- ) <+>
- WB.Layout.fixed(conatainer_app_content)
-
-server = one_page_server("Hello Bindings", container_app)
+ | {some={Record = [("username", username), ("message", message)]}} ->
+ line = <div class="line">
+ <div class="user">{Json.to_string(username)}:</>
+ <div class="message">{Json.to_string(message)}</>
+ </>
+ do Dom.transform([#conversation +<- line ])
+ Dom.scroll_to_bottom(#conversation)
+ | _ -> void
+
+broadcast(author, msg) =
+ username : RPC.Json.json = {String = author}
+ message : RPC.Json.json = {String = msg}
+ record_json : RPC.Json.json = {Record = [("username", username), ("message", message)]}
+ do PubNub.publish("chat", record_json)
+ Dom.clear_value(#entry)
+
+launch(author) =
+ init_client() =
+ //PubNub.history("chat", 20, (history ->
+ // history = List.rev(List.take(20, history))
+ // do List.iter(user_update, history)
+ PubNub.subscribe("chat", (x -> user_update(x)))
+ send_message() =
+ broadcast(author, Dom.get_value(#entry))
+ logout() =
+ do broadcast("system", "{author} has left the room")
+ Dom.transform([#main <- <a class="button" href="/">Reconnect</a>])
+ <div id=#header><div id=#logo></div>
+ <div class="button" onclick={_ -> logout()}>Logout</div>
+ </div>
+ <div id=#conversation onready={_ -> init_client()}></div>
+ <input id=#entry onnewline={_ -> send_message()}/>
+ <div class="button" onclick={_ -> send_message()}>Send</div>
+
+start() =
+ go(_) =
+ author = Dom.get_value(#author)
+ do Dom.transform([#main <- launch(author)])
+ broadcast("system", "{author} is connected to the room")
+ <div id=#main>
+ <div id=#header><div id=#logo></div>Choose your name:<input id=#author onnewline={go}/></div>
+ <div class="button" onclick={go}>Launch</div>
+ </div>
+
+server = Server.one_page_bundle("Chat",
+ [@static_resource_directory("src/examples/resources")],
+ ["src/examples/resources/css.css"], start)
@@ -1,93 +0,0 @@
-
-import pubnub
-
-import stdlib.themes.bootstrap
-import stdlib.widgets.bootstrap
-
-
-action() =
- s = Dom.get_value(#input)
- username : RPC.Json.json = {String = "tristan"}
- message : RPC.Json.json = {String = s}
- record_json : RPC.Json.json = {Record = [("username", username), ("message", message)]}
- PubNub.publish("chat", record_json)
-
-button(id, message) =
- <a id={id:string}
- class="button"
- ref="#"
- onclick={_ -> action() }>{message:string}
- </a>
-
-add(x) =
- match Json.deserialize(x) with
- | {some={Record = [("username", username), ("message", message)]}} -> Dom.transform([ #box +<- Json.to_string(message) ])
- | {some=_} -> Dom.transform([ #box <- "fail" ])
- | {none} -> Dom.transform([ #box <- "none" ])
-
-page() =
- <>
- <div id=pubnub pub-key=demo sub-key=demo onready={_ -> PubNub.subscribe("chat", (x -> add(x))) }></div>
- <div><input id=input placeholder=you-chat-here /></div>
- <div id=box></div>
- {button("publish_client", "Send Message")}
- </>
-
-WB = WBootstrap
-
-common_unit =
- <div class="hero-unit">
- <h1>Hello, world!</h1>
- <p>Vestibulum id ligula porta felis euismod semper. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
- <p><a class="btn primary large">Learn more »</a></p>
- </div>
-
-common_row = WB.Grid.row([
- {span=6 offset=none content=
- <h2>Heading</h2>
- <p>Etiam porta sem malesuada magna mollis euismod. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>
- <p><a class="btn" href="#">View details »</a></p>},
- {span=5 offset=none content=
- <h2>Heading</h2>
- <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
- <p><a class="btn" href="#">View details »</a></p>},
- {span=5 offset=none content=
- <h2>Heading</h2>
- <p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
- <p><a class="btn" href="#">View details »</a></p>}
-])
-
-common_footer =
- <footer>
- <p>© Company 2011</p>
- </footer>
-
-conatainer_app_content =
- WB.Div.content(
- WB.Div.page_header(1, "Page name", some("Supporting text or tagline")) <+>
- WB.Grid.row([
- {span=10 offset=none content=<div><h2>Chat</h2><ul id="box"></ul><div id=pubnub pub-key=demo sub-key=demo onready={_ -> PubNub.subscribe("chat", (x -> add(x))) }></div><div><input id=input placeholder=you-chat-here /></div>{button("publish_client", "Send Message")}</div>},
- {span=4 offset=none content=<h3>Users</h3>}
- ])
- ) <+>
- common_footer
-
-container_app() =
- WB.Navigation.topbar(
- WB.Layout.fixed(
- WB.Navigation.brand(<>Project name</>, some("/container-app"), ignore) <+>
- WB.Navigation.nav([
- {active=<>Home</> href=some("#") onclick=ignore},
- {inactive=<>About</> href=some("#about") onclick=ignore},
- {inactive=<>Contact</> href=some("#contact") onclick=ignore}
- ]) <+>
- (<form action="">
- <input class="input-small" type="text" placeholder="Username"/>
- <input class="input-small" type="password" placeholder="Password"/>
- <button class="btn" type="submit">Sign in</button>
- </form> |> WB.pull_right(_))
- )
- ) <+>
- WB.Layout.fixed(conatainer_app_content)
-
-server = one_page_server("Hello Bindings", container_app)
Oops, something went wrong.

0 comments on commit 99365ad

Please sign in to comment.