Skip to content
This repository has been archived by the owner on Apr 1, 2018. It is now read-only.

closing socket if joining fails #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var ws
var myNick = localStorageGet('my-nick')
var myChannel = window.location.search.replace(/^\?/, '')
var lastSent = ""
var wasConnected = false

// Ping server every 50 seconds to retain WebSocket connection
window.setInterval(function() {
Expand All @@ -61,8 +62,6 @@ function join(channel) {
ws = new WebSocket('ws://' + document.domain + ':6060')
}

var wasConnected = false

ws.onopen = function() {
if (!wasConnected) {
myNick = prompt('Nickname:', myNick)
Expand All @@ -71,16 +70,15 @@ function join(channel) {
localStorageSet('my-nick', myNick)
send({cmd: 'join', channel: channel, nick: myNick})
}
wasConnected = true
}

ws.onclose = function() {
if (wasConnected) {
pushMessage('!', "Server disconnected. Attempting to reconnect...", Date.now(), 'warn')
window.setTimeout(function() {
join(channel)
}, 2000)
}
window.setTimeout(function() {
join(channel)
}, 2000)
}

ws.onmessage = function(message) {
Expand Down Expand Up @@ -115,6 +113,7 @@ var COMMANDS = {
nicks.forEach(function(nick) {
userAdd(nick)
})
wasConnected = true
pushMessage('*', "Users online: " + nicks.join(", "), Date.now(), 'info')
},
onlineAdd: function(args) {
Expand Down
4 changes: 4 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ var COMMANDS = {

if (POLICE.frisk(getAddress(this), 3)) {
send({cmd: 'warn', text: "You are joining channels too fast. Wait a moment and try again."}, this)
this.close()
return
}

Expand All @@ -123,6 +124,7 @@ var COMMANDS = {
nick = nick.trim()
if (nick.toLowerCase() == config.admin.toLowerCase()) {
send({cmd: 'warn', text: "Cannot impersonate the admin"}, this)
this.close()
return
}
if (nick == config.password) {
Expand All @@ -131,6 +133,7 @@ var COMMANDS = {
}
if (!nicknameValid(nick)) {
send({cmd: 'warn', text: "Nickname invalid"}, this)
this.close()
return
}

Expand All @@ -139,6 +142,7 @@ var COMMANDS = {
if (client.channel === channel) {
if (client.nick.toLowerCase() === nick.toLowerCase()) {
send({cmd: 'warn', text: "Nickname taken"}, this)
this.close()
return
}
}
Expand Down