1. --!movie
  2. --!encoding=utf-8
  3.  
  4. global $
  5.  
  6. -- config
  7. global BASEURL
  8. global ALIVE_SEC
  9.  
  10. -- ID
  11. global gMyID
  12.  
  13. -- receive
  14. global gReceiveCh
  15.  
  16. ----------------------------------------
  17. --
  18. ----------------------------------------
  19. on startMovie
  20.  
  21.   -- libs
  22.   $.import("curl")
  23.   $.import("json")
  24.   $.import("timer")
  25.  
  26.   -- config
  27.   BASEURL = "https://valentin.dasdeck.com/projects/https_chat/"
  28.   ALIVE_SEC = 180
  29.  
  30.   -- window settings
  31.   w = 640
  32.   h = 480
  33.   _movie.stage.title = "HTTPS Chat"
  34.   _movie.stage.titlebarOptions.visible = TRUE
  35.   _movie.stage.rect = rect(0,0,w,h)
  36.   _movie.centerStage = TRUE
  37.   _movie.exitLock = TRUE
  38.  
  39.   -- create "users" field
  40.   m = new(#field)
  41.   m.border = 1
  42.   m.boxType = #scroll
  43.   m.margin = 1
  44.   m.name = "users"
  45.   m.rect = rect(0,0,120-16,460)
  46.   m.alignment = "left"
  47.   m.fontsize = 12
  48.   sprite(1).puppet = TRUE
  49.   sprite(1).member = m
  50.   sprite(1).loc = point(10,10)
  51.  
  52.   -- create "messages" field
  53.   m = new(#field)
  54.   m.border = 1
  55.   m.boxType = #scroll
  56.   m.margin = 1
  57.   m.name = "messages"
  58.   m.rect = rect(0,0,490-16,380)
  59.   m.alignment = "left"
  60.   m.fontsize = 12
  61.   sprite(2).puppet = TRUE
  62.   sprite(2).member = m
  63.   sprite(2).loc = point(140,10)
  64.  
  65.   -- create "msg" field
  66.   m = new(#field)
  67.   m.editable = TRUE
  68.   m.border = 1
  69.   m.boxType = #scroll
  70.   m.margin = 1
  71.   m.name = "msg"
  72.   m.rect = rect(0,0,490-16,40)
  73.   m.alignment = "left"
  74.   m.fontsize = 12
  75.   m.scriptText = "on keyDown"&RETURN&\
  76.   "  if _key.keyCode=36 then sendMessage()"&RETURN&\
  77.   "  else pass()"&RETURN&\
  78.   "end"
  79.   sprite(3).puppet = TRUE
  80.   sprite(3).member = m
  81.   sprite(3).loc = point(140,400)
  82.  
  83.   -- create send button
  84.   m = new(#button)
  85.   m.rect = rect(0,0,120,40)
  86.   m.text = "Send Message"
  87.   m.alignment = "center"
  88.   m.fontsize = 11
  89.   m.fontStyle = "bold"
  90.   m.scriptText = "on mouseUp"&RETURN&\
  91.   "  sendMessage()"&RETURN&\
  92.   "end"
  93.   sprite(4).puppet = TRUE
  94.   sprite(4).member = m
  95.   sprite(4).loc = point(140,450)
  96.  
  97.   -- show the window
  98.   _movie.stage.visible = 1
  99.  
  100.   -- force immediate stage update
  101.   _movie.updateStage()
  102.  
  103.   -- create alive timeout
  104.   $.timer.create(ALIVE_SEC*1000, #slotSendAlive)
  105.  
  106.   connect()
  107. end
  108.  
  109. ----------------------------------------
  110. -- @callback
  111. ----------------------------------------
  112. on closeRequest
  113.   if not voidP(gMyID) then
  114.     disconnect()
  115.   end if
  116.   _movie.halt()
  117. end
  118.  
  119. ----------------------------------------
  120. --
  121. ----------------------------------------
  122. on connect ()
  123.   _ch = $.curl.init()
  124.   _ch.setOption($.curl.CURLOPT.URL, BASEURL&"?connect")
  125.   _ch.setOption($.curl.CURLOPT.SSL_VERIFYPEER, 0)
  126.   res = _ch.exec(1)
  127.   if integerP(res) then
  128.     put "ERROR:" && curl_error(res)
  129.   else
  130.     res = res.readRawString(res.length)
  131.     gMyID = integer(res)
  132.     -- start HTTP long polling message receive loop
  133.     reconnect()
  134.   end if
  135. end
  136.  
  137. ----------------------------------------
  138. --
  139. ----------------------------------------
  140. on disconnect ()
  141.   _ch = $.curl.init()
  142.   _ch.setOption($.curl.CURLOPT.URL, BASEURL&"?disconnect="&gMyID)
  143.   _ch.setOption($.curl.CURLOPT.SSL_VERIFYPEER, 0)
  144.   _ch.exec()
  145. end
  146.  
  147. ----------------------------------------
  148. --
  149. ----------------------------------------
  150. on showUserList (users)
  151.   str = ""
  152.   repeat with user in users
  153.     put user&RETURN after str
  154.   end repeat
  155.   delete the last char of str
  156.   member("users").color = rgb(0,0,0)
  157.   member("users").text = str
  158.   -- hilite myself
  159.   pos = users.getPos(gMyID)
  160.   if pos then member("users").line[pos].color=rgb(255,0,0)
  161. end
  162.  
  163. ----------------------------------------
  164. --
  165. ----------------------------------------
  166. on showNewMessages (msg)
  167.   m = member("messages")
  168.   if m.text=EMPTY then
  169.     n = 0
  170.   else
  171.     n = m.line.count
  172.   end if
  173.   repeat with row in msg
  174.     n = n+1
  175.     m.line[n] = row.peer_id&": "&row.data
  176.     if row.peer_id=gMyID then
  177.       -- hilite myself
  178.       m.line[n].color=rgb(255,0,0)
  179.     else
  180.       m.line[n].color=rgb(0,0,0)
  181.     end if
  182.   end repeat
  183. end
  184.  
  185. ----------------------------------------
  186. --
  187. ----------------------------------------
  188. on sendMessage ()
  189.   msg = member("msg").text
  190.   if msg=EMPTY then return
  191.   _ch = $.curl.init()
  192.   _ch.setOption($.curl.CURLOPT.URL, BASEURL&"?id="&gMyID&"&msg="&curl_escape(msg))
  193.   _ch.setOption($.curl.CURLOPT.SSL_VERIFYPEER, 0)
  194.   _ch.exec()
  195.   member("msg").text = ""
  196. end
  197.  
  198. ----------------------------------------
  199. --
  200. ----------------------------------------
  201. on reconnect()
  202.   gReceiveCh = VOID
  203.   gReceiveCh = $.curl.init()
  204.   gReceiveCh.setOption($.curl.CURLOPT.URL, BASEURL&"?id="&gMyID)
  205.   gReceiveCh.setOption($.curl.CURLOPT.SSL_VERIFYPEER, 0)
  206.   $.curl.execAsyncDetached(gReceiveCh, #slotCurlResult, _movie, 1)
  207. end
  208.  
  209. ----------------------------------------
  210. -- @callback
  211. ----------------------------------------
  212. on slotSendAlive ()
  213.   _ch = $.curl.init()
  214.   _ch.setOption($.curl.CURLOPT.URL, BASEURL&"?alive="&gMyID)
  215.   _ch.setOption($.curl.CURLOPT.SSL_VERIFYPEER, 0)
  216.   _ch.exec()
  217. end
  218.  
  219. ----------------------------------------
  220. -- @callback
  221. ----------------------------------------
  222. on slotCurlResult (res)
  223.   if integerP(res) then
  224.     put "ERROR:" && curl_error(res)
  225.   else
  226.     str = res.readRawString(res.length)
  227.     if str starts "list:" then
  228.       users = $.json.decode(str.char[6..str.length])
  229.       showUserList(users)
  230.     else if str starts "msg:" then
  231.       msg = $.json.decode(str.char[5..str.length])
  232.       showNewMessages(msg)
  233.     end if
  234.   end if
  235.   reconnect()
  236. end
  237.  
[raw code]