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 18th, 2011  |  syntax: None  |  size: 2.14 KB  |  views: 32  |  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.  
  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.         room = Network.cloud(roomname): Network.network(message)
  24.         user = String.to_lower(Dom.get_value(#user))      
  25.        
  26.         do notify_room(room, user)
  27.  
  28.         callback = user_update(_, user)
  29.         line =
  30.              <h1>Room {roomname}</h1><hr />
  31.              <div id=#conversation onready={_ -> Network.add_callback(callback, room)} />
  32.              <input id=#user type="hidden" value="{user}" />
  33.              <input id=#entry onnewline={_ -> broadcast(room, user)} />
  34.              <input type="button" value="Send" onclick={_ -> broadcast(room, user)} />
  35.         do Dom.transform([#main <- line])
  36.         void
  37. )
  38.  
  39. notify_room(room, user) =
  40. (
  41.         date = Date.now()
  42.         do Network.broadcast({user="SERVER"
  43.                               text="User '{user}' joined the room."
  44.                               date="{Date.get_hour(date)}:{Date.get_min(date)}"}, room)                          
  45.         void
  46. )
  47.  
  48. broadcast(room, user) =
  49. (
  50.         text = Dom.get_value(#entry)
  51.         date = Date.now()
  52.         message = {~user ~text date="{Date.get_hour(date)}:{Date.get_min(date)}"}
  53.         do Network.broadcast(message, room)
  54.         Dom.clear_value(#entry)
  55. )
  56.  
  57. user_update(x: message, current_user: string) =
  58. (
  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)
clone this paste RAW Paste Data