Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 106 lines (96 sloc) 2.75 kb
14e61d3 @akoprow Initial code of the hello_chat manual tutorial.
akoprow authored
1 /**
2 * {1 Import standard classes of bootstrap css}
3 *
4 * see http://twitter.github.com/bootstrap/
5 */
6 import stdlib.themes.bootstrap
7
8 /**
9 * {1 Network infrastructure}
10 */
11
12 /**
13 * The type of messages sent by a client to the chatroom
14 */
15 type message = { string author /**The name of the author (arbitrary string)*/
16 , string text /**Content entered by the user*/
17 }
18
19 /**
20 * The chatroom.
21 */
22 // FIXME mixed directives; will need to be cleaned-up
23 exposed @async room = Network.network(message) (Network.cloud("room"))
24
25 /**
26 * {1 User interface}
27 */
28
29 /**
30 * Update the user interface in reaction to reception of a message.
31 *
32 * This function is meant to be registered with [room] as a callback.
33 * Its sole role is to display the new message in [#conversation].
34 *
35 * @param x The message received from the chatroom
36 */
37 function user_update(message x) {
38 line = <div class="row line">
39 <div class="span1 columns userpic" />
40 <div class="span2 columns user">{x.author}:</div>
41 <div class="span13 columns message">{x.text}
42 </div>
43 </div>
44 #conversation =+ line
45 Dom.scroll_to_bottom(#conversation)
46 }
47
48 /**
49 * Broadcast text to the [room].
50 *
51 * Read the contents of [#entry], clear these contents and send the message to [room].
52 *
53 * @param author The name of the author. Will be included in the message broadcasted.
54 */
55 function broadcast(author) {
56 text = Dom.get_value(#entry)
57 message = ~{author, text}
58 Network.broadcast(message, room)
59 Dom.clear_value(#entry)
60 }
61
62 /**
63 * Build the user interface for a client.
64 *
65 * Pick a random author name which will be used throughout the chat.
66 *
67 * @return The user interface, ready to be sent by the server to the client on connection.
68 */
69 function start() {
70 author = Random.string(8)
71 <div class="topbar">
72 <div class="fill">
73 <div class="container">
74 <div id=#logo />
75 </>
76 </>
77 </>
78 <div id=#conversation class="container"
79 onready={function(_) { Network.add_callback(user_update, room) }}></>
80 <div id=#footer>
81 <div class="container">
82 <input id=#entry class="xlarge" onnewline={function(_) { broadcast(author) }} />
83 <div class="btn primary" onclick={function(_) { broadcast(author) }}>Post</>
84 </>
85 </>
86 }
87
88 /**
89 * {1 Application}
90 */
91
92 /**
93 * Main entry point.
94 *
95 * Construct an application called "Chat" (users will see the name in the title bar),
96 * embedding statically the contents of directory "resources", using the global stylesheet
97 * "resources/css.css" and the user interface defined in [start].
98 */
99 Server.start(Server.http,
100 { title: "Chat"
101 , resources: @static_resource_directory("resources")
102 , css: ["resources/css.css"]
103 , page: start
104 }
105 )
Something went wrong with that request. Please try again.