Permalink
Browse files

Client-side commands (/quit, /join)

  • Loading branch information...
1 parent ad6fe72 commit ae5ea92b9360de05983abe67da9784914008471c @jaspervdj committed Dec 9, 2011
Showing with 48 additions and 13 deletions.
  1. +40 −13 client.coffee
  2. +2 −0 index.html
  3. +6 −0 screen.css
View
@@ -8,6 +8,9 @@ createWebSocket = (path) ->
else
new WebSocket(uri)
+error = (error) ->
+ $('#errors').text(error)
+
class Tab
constructor: (@tabManager, @name, @channel) ->
@tab = tab = this
@@ -103,6 +106,10 @@ class TabManager
@channelTabs[channel] = new Tab(@tabManager, channel, channel)
@channelTabs[channel]
+disconnect = (ws) ->
+ ws.send(JSON.stringify({"type": "disconnect"}))
+ location.reload(true)
+
makeHandlers = (tabManager, nick) ->
log: (event) ->
tab = tabManager.getServerTab()
@@ -134,13 +141,25 @@ makeHandlers = (tabManager, nick) ->
ready: (event) ->
$('#join-submit').removeAttr('disabled')
+makeCommands = (tabManager, nick, ws) ->
+ quit: (msg) ->
+ disconnect(ws)
+
+ join: (msg) ->
+ ws.send(JSON.stringify({
+ 'type': 'join',
+ 'channel': msg,
+ 'nick': nick
+ }))
+
connect = (server, port, nick) ->
ws = createWebSocket('/chat')
$('#connect').hide()
$('#chat').show()
tabManager = new TabManager()
handlers = makeHandlers(tabManager, nick)
+ commands = makeCommands(tabManager, nick, ws)
serverTab = tabManager.getServerTab()
tabManager.showTab(serverTab)
@@ -169,11 +188,7 @@ connect = (server, port, nick) ->
ws.onopen = () ->
ws.send(JSON.stringify({"type": "connect", "user": user}))
- $('#disconnect-form').submit(() ->
- ws.send(JSON.stringify({"type": "disconnect"}))
- location.reload(true)
- false
- )
+ $('#disconnect-form').submit(() -> disconnect(ws))
$('#join-form').submit(() ->
channel = $('#join-channel').val()
@@ -188,18 +203,30 @@ connect = (server, port, nick) ->
$('#send-form').submit(() ->
channel = tabManager.getActiveTab().getChannel()
text = $('#send-text').val()
- if channel
- ws.send(JSON.stringify({
- "type": "privmsg",
- "channel": channel,
- "nick": nick,
- "text": text
- }))
- $('#send-text').val('')
+
+ if matches = text.match(/^\/\w+/)
+ cmd = matches[0].substr(1)
+ argument = text.substr(2 + cmd.length)
+ if commands[cmd]
+ commands[cmd](argument)
+ else
+ error("Unknown command #{cmd}")
+
+ else
+ if channel
+ ws.send(JSON.stringify({
+ "type": "privmsg",
+ "channel": channel,
+ "nick": nick,
+ "text": text
+ }))
+
+ $('#send-text').val('')
false
)
$(document).ready(() ->
+ error('Some dummy error')
$('#connect-form').submit(() ->
try
server = $('#connect-server').val()
View
@@ -50,5 +50,7 @@
<input type="submit" value="Send" />
</div>
</div>
+ <div id="errors">
+ </div>
</body>
</html>
View
@@ -63,3 +63,9 @@ div#tab-buttons div.alert {
form#send-form {
padding: 6px;
}
+
+div#errors {
+ padding: 6px;
+ font-weight: bold;
+ color: #d00;
+}

0 comments on commit ae5ea92

Please sign in to comment.