forked from jfmwz/pollchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pollchat-server.js
executable file
·95 lines (78 loc) · 2.6 KB
/
pollchat-server.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/node
//
// Very simple kafka based chat client for node JS
//
// Basically a less crappy functional test than the can-fake-with-netcat one that
// comes with kafka
//
// David Basden <davidb@anchor.net.au>
//
"use strict";
//============================================//
var listenport = 31337;
var kafkahost = 'kafka.example.com';
var kafkaport = 9092;
var kafkatopic = 'chatroom';
var kafkapartition = 0;
//============================================//
var WebSocketServer = require('websocket').server;
var http = require('http');
// Logging
function ln(msg) { console.log( (new Date()) + msg ); }
// Setup HTTP listener
var server = http.createServer(function(request,response) { });
server.listen(listenport, function() {});
// Websockets server
//
var wsServer = new WebSocketServer( { httpServer: server });
wsServer.on('request', function(req) {
ln("New connection from " + req.origin + "...");
var conn = req.accept(null,req.origin);
ln("... accepted. Connecting to kafka...");
var kafka = require('kafka');
// Connect to kafka as a producer to be able to send messages to it
var producer = new kafka.Producer({
host: kafkahost,
port: kafkaport,
topic: kafkatopic,
partition: kafkapartition,
}).connect().on('connect', function() {
ln("Connected to kafka as a producer for "+conn.remoteAddress);
})
ln("connected.");
// Handle messages from the client
conn.on('message', function(msg) {
if (msg.type === 'utf8') { // Only bother with text messages
var data = msg.utf8Data;
ln("Got message from client "+conn.remoteAddress+": " + data);
// conn.sendUTF(data);
//ln("echoed.");
producer.send(data);
ln(" ---> Relayed message to kafka: " + data)
}
});
conn.on('error', function(err) { ln("websocketserver is throwing an error"); });
// Connect to kafka and handle messages from the chatroom topic
var consumer = new kafka.Consumer({
host: kafkahost,
port: kafkaport
});
consumer.connect().subscribeTopic({
name: kafkatopic,
partition: kafkapartition
}).on('message', function(topic, message) {
ln("Got message from kafka: "+ message);
conn.sendUTF(message);
ln(" ---> Relayed message to client: "+ message);
});
consumer.on('error', function(err,more) { ln("consumer is throwing an error"); ln(err.toString()); });
producer.on('error', function(err) { ln("producer is throwing an error"); ln(err.toString());});
// Handle client disconnections
conn.on('close', function(conn) {
ln(conn.remoteAddress + " disconnected")
ln("closing kafka consumer");
consumer.close();
ln("closing kafka producer");
producer.close();
} );
});