Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0a115af782
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 58 lines (46 sloc) 1.764 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
tojson: JSON.stringify
fromjson: JSON.parse
ctr: window.webkitNotifications

controls: -> $("#controls *")
disable: (controls) -> controls.attr "disabled", "disabled"
enable: (controls) -> controls.removeAttr "disabled"

notify: (title, text)->
  if ctr.checkPermission() is 0
    notification: ctr.createNotification(null, title, text)
    notification.show()

announce: (text) ->
  new_msg: $("<div>").addClass("announcement").text(text)
  $('#chatwindow').append new_msg

message: (you, msg) ->
  if not you then notify "Stranger", msg
  prefix: if you then "You:" else "Stranger:"
  prefix_class: if you then "mymessage" else "strangermessage"
  
  new_msg: $("<div>").addClass("announcement").
              append($("<div>").text(prefix).addClass(prefix_class)).
              append($("<div>").text(msg).addClass("message"))
  $('#chatwindow').append new_msg
  
handle_message: (data) ->
  switch data.type
    when "connect"
      enable controls()
      announce "You're now chatting with a random stranger. Say hey!"
    when "disconnect"
      disable controls()
      announce "disconnected :("
    when "message"
      message data.you, data.msg

$(document).ready ->
  disable controls()
  
  switch ctr?.checkPermission()
    when 1, 2 then ctr.requestPermission notify

  socket: new io.Socket null, {rememberTransport: false, port: 80}
  console.log socket
  socket.connect()
  socket.send tojson {'type': 'wantpartner'}
  socket.addEvent 'message', (data) -> handle_message fromjson data

  $('#disconnectbtn').click ->
    socket.send tojson {'type': 'wantdisconnect'}

  $('#sendbtn').click ->
    if $('#textarea').val().length > 0
      socket.send tojson {'type': 'message', 'msg': $('#textarea').val()}
      $('#textarea').val("").focus()
Something went wrong with that request. Please try again.