This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Extended hello_chat with Opa

By: bahman on Oct 17th, 2011  |  syntax: None  |  size: 2.11 KB  |  views: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Extended hello_chat
  2. // Bahman Movaqar
  3. import stdlib.core.date
  4.  
  5.  
  6. type message = {user: string; text: string; date: string;}
  7.  
  8. start() =
  9. (
  10.         <div id=#main>
  11.              <h1>Welcome to Extended Hello Chat Application!</h1><hr />
  12.              <table>
  13.                 <tr><td>Username:</td><td><input id=#user /></td></tr>
  14.                 <tr><td>Room:</td><td><input id=#room /></td></tr>
  15.                 <tr><td colspan="2"><input type="button" onclick={_ -> join_room()} value="Join" /></td></tr>
  16.              </table>
  17.         </div>
  18. )
  19.  
  20. join_room() =
  21. (
  22.         roomname = Dom.get_value(#room)
  23.         user = String.to_lower(Dom.get_value(#user))
  24.         room = Network.cloud(roomname): Network.network(message)
  25.        
  26.         do notify_room(room, user)        
  27.  
  28.         line =
  29.              <h1>Room {roomname}</h1><hr />
  30.              <div id=#conversation onready={_ -> Network.add_callback(user_update, room)} />
  31.              <input id=#entry onnewline={_ -> broadcast(room, user)} />
  32.              <input type="button" value="Send" onclick={_ -> broadcast(room, user)} />
  33.         do Dom.transform([#main <- line])
  34.         void
  35. )
  36.  
  37. notify_room(room, user) =
  38. (
  39.         date = Date.now()
  40.         do Network.broadcast(
  41.            {user="SERVER" text="User {user} joined the room." date="{Date.get_hour(date)}:{Date.get_min(date)}"},
  42.            room)
  43.         void
  44. )
  45.  
  46. broadcast(room, user) =
  47. (
  48.         text = Dom.get_value(#entry)
  49.         date = Date.now()
  50.         message = {~user ~text date="{Date.get_hour(date)}:{Date.get_min(date)}"}
  51.         do Network.broadcast(message, room)
  52.         Dom.clear_value(#entry)
  53. )
  54.  
  55. user_update(x: message) =
  56. (
  57.         // The following two lines are trying to mimic a session-wide variable unsuccessfully.
  58.         current_user = String.to_lower(Dom.get_value(#user))
  59.         line =
  60.              <div>{x.date} <b>{if current_user == x.user then "You" else x.user}</b>: {x.text}</div>
  61.         do Dom.transform([#conversation +<- line])
  62.         Dom.scroll_to_bottom(#conversation)
  63. )
  64.  
  65. server = Server.one_page_bundle("Extended Hello Chat With Opa", [], [], start)
  66.  
  67.  
clone this paste RAW Paste Data