summaryrefslogtreecommitdiffstats
path: root/webchat/hello_web.js
diff options
context:
space:
mode:
authorlassulus <lassulus@googlemail.com>2013-11-05 19:50:49 +0100
committerlassulus <lassulus@googlemail.com>2013-11-05 19:50:49 +0100
commitff67c4b7215358cf785c058201328ff6a0f5fc2b (patch)
treed04369c2884c9da79e339d5cb49dc9f508c7af14 /webchat/hello_web.js
parent1d69388a39964c67a2e17483084565279e1fb550 (diff)
webchat: now in git
Diffstat (limited to 'webchat/hello_web.js')
-rw-r--r--webchat/hello_web.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/webchat/hello_web.js b/webchat/hello_web.js
new file mode 100644
index 00000000..31e106c7
--- /dev/null
+++ b/webchat/hello_web.js
@@ -0,0 +1,83 @@
+var fs = require('fs');
+var http = require('https');
+var sockjs = require('sockjs');
+var connect = require('connect');
+var irc = require('irc');
+var krebsimage = require('./krebs-img.js');
+var Clients = [];
+Clients.broadcast = function(object) {
+ Clients.forEach(function(client) {
+ client.write(JSON.stringify(object));
+ });
+}
+
+var irc_client = new irc.Client('irc.freenode.net', 'kweb', {
+ channels: ['#krebs'],
+ sasl: true,
+ secure: true,
+ userName: 'kweb',
+ realName: 'kweb',
+ password: fs.readFileSync(__dirname+'/irc.key').toString(),
+ debug: true,
+ showErrors: true,
+ port: 6697,
+});
+
+irc_client.on('message#krebs', function(from, message) {
+ console.log({ from: from, message: message });
+ Clients.broadcast({ from: from, message: message });
+});
+
+
+var echo = sockjs.createServer();
+echo.on('connection', function(conn) {
+ var name = '['+conn.remoteAddress+':'+conn.remotePort+']';
+ Clients.push(conn);
+ Clients.broadcast({from: 'system', message: name + ' has joined'})
+ irc_client.say("#krebs", name + ' has joined');
+conn.write(JSON.stringify({from: 'system', message: 'hello'}))
+ conn.on('data', function(message) {
+ console.log('data:',message);
+ try {
+ var object = JSON.parse(message);
+ object.from = name
+ console.log(object.message);
+ irc_client.say("#krebs", name + '→' + object.message);
+ Clients.broadcast(object);
+
+ } catch (error) {
+ console.log(error);
+ }
+ });
+conn.on('close', function() {
+ Clients.splice(Clients.indexOf(conn));
+ Clients.broadcast({from: 'system', message: name + ' has quit'})
+ irc_client.say("#krebs", name + ' has quit');
+});
+});
+
+var options = {
+ key: fs.readFileSync(__dirname+'/server_npw.key'),
+ cert: fs.readFileSync(__dirname+'/server.crt'),
+};
+
+var app = connect()
+ .use(connect.logger('dev'))
+ .use(connect.static(__dirname+'/public'))
+ .use( function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.write('<!doctype html>');
+ res.write('<link rel="stylesheet" type="text/css" href="reset.css">');
+ res.write('<script src="sockjs-0.3.min.js"></script>');
+ res.write('<script src="jquery-2.0.3.min.js"></script>');
+ res.write('<script src="client.js"></script>');
+ res.write(krebsimage+'<br>');
+ res.write('hello, this is result:<br>');
+ res.write('<table id="chatbox"></table>');
+ res.end('<input type="text" id="input"><br>');
+
+ })
+var server = http.createServer(options, app);
+echo.installHandlers(server, {prefix:'/echo'});
+server.listen(1337, '0.0.0.0');
+console.log('Server running at https://127.0.0.1:1337/');