Permalink
Browse files

added pubnub module and chat module

  • Loading branch information...
1 parent 6a31837 commit 668ea566e6eafb8a938e54026a85ffad975edf83 @tsloughter tsloughter committed Nov 13, 2011
Showing with 201 additions and 22 deletions.
  1. +1 −1 Makefile
  2. +93 −0 src/examples/pubnub_chat.opa
  3. +70 −21 src/examples/pubnub_test.opa
  4. +37 −0 src/plugin/pubnub.opa
View
@@ -1,6 +1,6 @@
NAME = pubnub_test.exe
-SRC = pubnub.opp src/examples/pubnub_test.opa
+SRC = pubnub.opp src/plugin/pubnub.opa src/examples/pubnub_chat.opa src/examples/pubnub_test.opa
all : pubnub opa
@@ -0,0 +1,93 @@
+
+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)
@@ -1,44 +1,93 @@
-pubnub_publish = %%pubnub.publish%%
-pubnub_subscribe = %%pubnub.subscribe%%
-pubnub_history = %%pubnub.history%%
-@client publish_client = pubnub_publish
-@client subscribe_client = pubnub_subscribe
-@client history_client = pubnub_history
+import pubnub
+
+import stdlib.themes.bootstrap
+import stdlib.widgets.bootstrap
-action() =
- history_client("chat", 10, (x -> Dom.transform([ #box <- x])))
-/* s = Dom.get_value(#input)
+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)]}
- publish(record_json)
-*/
-
-publish(j : RPC.Json.json) =
- publish_client("chat", Json.to_string(j))
+ PubNub.publish("chat", record_json)
button(id, message) =
- <a id={id:string}
+ <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={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={_ -> subscribe_client("chat", (x -> add(x))) }></div>
+ <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")}
+ <div id=box></div>
+ {button("publish_client", "Send Message")}
</>
-do Resource.register_external_js("http://cdn.pubnub.com/pubnub-3.1.min.js")
-server = one_page_server("Hello Bindings", page)
+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)
View
@@ -0,0 +1,37 @@
+package pubnub
+
+pubnub_publish = %%pubnub.publish%%
+pubnub_subscribe = %%pubnub.subscribe%%
+pubnub_unsubscribe = %%pubnub.unsubscribe%%
+pubnub_history = %%pubnub.history%%
+pubnub_uuid = %%pubnub.uuid%%
+pubnub_time = %%pubnub.time%%
+
+@client publish_client = pubnub_publish
+@client subscribe_client = pubnub_subscribe
+@client unsubscribe_client = pubnub_unsubscribe
+@client history_client = pubnub_history
+@client uuid_client = pubnub_uuid
+@client time_client = pubnub_time
+
+PubNub = {{
+ publish(channel: string, j : RPC.Json.json) =
+ publish_client(channel, Json.to_string(j))
+
+ subscribe(channel: string, callback) =
+ subscribe_client(channel, callback)
+
+ unsubscribe(channel: string) =
+ unsubscribe_client(channel)
+
+ history(channel: string, limit: int, callback) =
+ history_client(channel, limit, callback)
+
+ uuid(callback) =
+ uuid_client(callback)
+
+ time(callback) =
+ time_client(callback)
+}}
+
+do Resource.register_external_js("http://cdn.pubnub.com/pubnub-3.1.min.js")

0 comments on commit 668ea56

Please sign in to comment.