Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Extended hello_chat with Opa

By: bahman on Oct 22nd, 2011  |  syntax: None  |  size: 2.32 KB  |  views: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. 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. type user_message = {user: string; text: string; date: Date.date;}
  6. type server_message = {text: string; date: Date.date;}
  7.  
  8. start() =
  9. (
  10.   <div id=#main>
  11.     <h1>Welcome!</h1>
  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.   usernet = Network.cloud("CHAT_USER_NETWORK")
  23.   servernet = Network.cloud("CHAT_SERVER_NETWORK")
  24.   room_name = Dom.get_value(#room)
  25.   server_channel = room_name
  26.   do Network.add(room_name, usernet)
  27.   do Network.add(server_channel, servernet)
  28.  
  29.   user = String.to_lower(Dom.get_value(#user))
  30.   do notify_room_join(server_channel, user)
  31.  
  32.   register_callbacks() = (
  33.     user_callback = room_update_user(_, user)
  34.     do Network.add_callback(user_callback, room_name)
  35.     do Network.add_callback(room_update_server, server_channel)
  36.     void
  37.   )
  38.   line =
  39.     <h1>Room {room_name}</h1><hr/>
  40.     <div id=#room onready={_ -> register_callbacks()}/>
  41.     <input id=#entry onnewline={_ -> broadcast_room(room_name, user)}/>
  42.     <input type="button" vlaue="Send" onclick={_ -> broadcast_room(room_name, user)}/>
  43.  
  44.   do Dom.transform([#room <- line])
  45.   void
  46. )
  47.  
  48. notify_room_join(server_channel, user) =
  49. (
  50.   date = Date.now()
  51.   m = {text="User '{user}' joined the room."; ~date}
  52.   do Network.broadcast(m, server_channel)
  53.   void
  54. )
  55.  
  56. broadcast_room(room, user) =
  57. (
  58.   text = Dom.get_value(#entry)
  59.   date = Date.now()
  60.   m = ~{user text date}
  61.   do Network.broadcast(m, room)
  62.   Dom.clear_value(#entry)
  63. )
  64.  
  65. room_update_server(m: server_message) =
  66. (
  67.   date = "{Date.get_hour(m.date)}:{Date.get_min(m.date)}:{Date.get_sec(m.date)}"
  68.   line = <div style="\{background: lightgray;\}">{date} {m.text}</div>
  69.   do Dom.transform([#room +<- line])
  70.   Dom.scroll_to_bottom(#room)
  71. )
  72.  
  73. room_update_user(m: user_message, user) =
  74. (
  75.   date="{Date.get_hour(m.date)}:{Date.get_min(m.date)}:{Date.get_sec(m.date)}"
  76.   line = <div>{date} <b>{if user == m.user then "YOU" else m.user}</b>: {m.text}</div>
  77.   do Dom.transform([#room +<- line])
  78.   Dom.scroll_to_bottom(#room)
  79. )
  80.  
  81. server = Server.one_page_bundle("Welcome", [], [], start)
  82.  
  83.  
clone this paste RAW Paste Data