Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First attempt of #nodester chat.

  • Loading branch information...
commit 0741b7ce65220f4ade8e6f91683288fea7b2ce25 1 parent d1c86ad
@DanBUK DanBUK authored
Showing with 67 additions and 2 deletions.
  1. +43 −1 irc.js
  2. +24 −1 public/index.html
View
44 irc.js
@@ -21,9 +21,51 @@ var socket = io.listen(app, {
});
socket.on('connection', function(client) {
+ var irc = null;
+ var nickname = null;
client.send(JSON.stringify({connected: true}));
client.on('message', function(data) {
- console.log(data);
+ var obj = JSON.parse(data);
+ if (obj.hasOwnProperty('nickname')) {
+ if (irc === null) {
+ nickname = obj.nickname;
+ irc = new ircjs({
+ server: 'holmes.freenode.net',
+ port: 6667,
+ nick: obj.nickname,
+ user: {
+ username: nickname,
+ hostname: 'irc.bejes.us',
+ servername: 'holmes.freenode.net',
+ realname: nickname + ' via http://irc.bejes.us/'
+ }
+ });
+ irc.connect(function () {
+ irc.join('#nodester');
+ });
+ irc.addListener('privmsg', function (message) {
+ if (message.params[0] == '#nodester') {
+ client.send(JSON.stringify({
+ type: "message",
+ from: message.person.nick,
+ channel: message.params[0],
+ message: message.params[1]
+ }));
+ } else {
+ irc.privmsg(message.person.nick, "I can only talk in #nodester.");
+ }
+ });
+ } else if (obj.hasOwnProperty('type')) {
+ switch (obj['type']) {
+ case "message":
+ irc.privmsg("#nodester", obj.message);
+ break;
+ default:
+ console.log(data);
+ break;
+ }
+ }
+ }
});
client.on('disconnect', function() {
View
25 public/index.html
@@ -34,11 +34,15 @@
var sock = null;
var rv = null;
var nickname = null;
+ var chatBody = null;
+ var textInput = null;
// var ts = null;
var body_loaded = function () {
sock = new io.Socket();
sock.on('message', handleMessage);
sock.connect();
+ chatBody = document.getElementById('chat_body');
+ textInput = document.getElementById('text_input');
};
var getNickname = function () {
var name = prompt("Please enter your nickname:", "");
@@ -67,9 +71,28 @@
rv = sock.send(JSON.stringify({ nickname: nickname }));
}
// rv = sock.send(JSON.stringify({ nickname: name }));
+ } else {
+ if (obj.hasOwnProperty('type')) {
+ switch (obj['type']) {
+ case "message":
+ var p = document.createElement('p');
+ p.innerHtml = "<b>" + obj.from + "</b>: " + obj.message;
+ chatBody.appendChild(p);
+ break;
+ default:
+ break;
+ }
+ }
}
// ts.textContent = obj.timestamp;
};
+ var sendMessage = function () {
+ sock.send(JSON.stringify({
+ type: "message",
+ message: textInput.value;
+ }));
+ textInput.value = "";
+ };
</script>
</head>
<body onload="body_loaded();">
@@ -84,7 +107,7 @@
</div>
</div>
<div id="chat_bar">
- <input type="text" id="text_input"></input><button>Send</button>
+ <input type="text" id="text_input"></input><button onClick="sendMessage();">Send</button>
</div>
</body>
</html>
Please sign in to comment.
Something went wrong with that request. Please try again.