-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.ts
157 lines (146 loc) · 4.7 KB
/
server.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import http = require("http");
import fs = require("fs");
import url = require("url");
declare var global:any;
global.EventSource = require('eventsource');
import {
ServerEventsClient,
ServerEventConnect,
ServerEventMessage,
ServerEventCommand,
splitOnFirst, splitOnLast
} from "servicestack-client";
import { PostChatToChannel, PostRawToChannel } from "./src/dtos";
var client:ServerEventsClient = null;
var CHANNEL = "";
var BASEURL = "";
var sub:ServerEventConnect = null;
var MESSAGES = [];
var USERS = [];
const refresh = (e:ServerEventMessage) => {
addMessage(e);
refreshUsers();
};
const refreshUsers = async () => {
var users = await client.getChannelSubscribers();
users.sort((x,y) => y.userId.localeCompare(x.userId));
USERS = users.map(x => ({
profileUrl: x.profileUrl,
displayName: x.displayName,
userId: x.userId
}));
};
const addMessage = (x:ServerEventMessage) =>
addMessageHtml(`<div><b>${x.selector}</b> <span class="json" title=${x.json}>${x.json}</span></div>`);
const addMessageHtml = (html:string) =>
(MESSAGES[CHANNEL] || (MESSAGES[CHANNEL] = [])).push(html);
const HANDLERS: { [index:string]: ((req:http.IncomingMessage, res:http.ServerResponse) => void) } = {
"/": (req, res) => handleFile("/index.html", res),
"/listen": (req,res) => {
const qs = url.parse(req.url, true).query;
if (client) {
client.stop();
client = null;
}
BASEURL = qs["baseUrl"];
CHANNEL = qs["channel"];
console.log(`Connecting to ${BASEURL} #${CHANNEL}...`);
client = new ServerEventsClient(BASEURL, [CHANNEL], {
handlers: {
onConnect: (e:ServerEventConnect) => {
refresh(sub = e);
},
onJoin: refresh,
onLeave: refresh,
onUpdate: refresh,
onMessage: (e:ServerEventMessage) => {
addMessage(e);
}
},
onException: e => {
addMessageHtml(`<div class="error">${e.message || e}</div>`);
}
}).start();
res.end();
},
"/state": (req, res) => {
var state = {
baseUrl: BASEURL,
channel: CHANNEL,
sub,
messages: (MESSAGES[CHANNEL] || ['<div class="error">NOT CONNECTED</div>']),
users: USERS
};
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(state));
},
"/chat": (req,res) => {
const qs = url.parse(req.url, true).query;
let request = new PostChatToChannel();
request.from = sub.id;
request.channel = CHANNEL;
request.selector = "cmd.chat";
request.message = qs["message"];
client.serviceClient.post(request);
res.end();
},
"/raw": (req,res) => {
const qs = url.parse(req.url, true).query;
var parts = splitOnFirst(qs["message"], " ");
if (!parts[0].trim()) return;
let request = new PostRawToChannel();
request.from = sub.id;
request.channel = CHANNEL;
request.selector = parts[0].trim();
request.message = parts.length == 2 ? parts[1].trim() : null;
client.serviceClient.post(request);
res.end();
}
};
const MimeTypes = {
"html": "text/html",
"css": "text/css"
}
const handleFile = (path:string, res:http.ServerResponse) => {
if (path.startsWith("/"))
path = path.substring(1);
fs.readFile(path, (err,data) => {
if (err) {
res.writeHead(400, err);
res.end();
return;
}
var parts = splitOnLast(path, ".");
if (parts.length != 2)
throw new Error("Unknown file extension for " + path);
var contentType = MimeTypes[parts[1]];
res.writeHead(200, {
"Content-Type": contentType,
"Content-Length": data.length
});
res.end(data);
});
}
const server = http.createServer((req,res) => {
var pathInfo = splitOnFirst(req.url, "?")[0];
var handler = HANDLERS[pathInfo];
if (handler) {
if (pathInfo != "/state")
console.log(`executing ${pathInfo}...`);
handler(req,res);
} else {
var path = pathInfo.startsWith("/")
? pathInfo.substring(1)
: pathInfo;
fs.exists(path, exists => {
if (exists) {
handleFile(pathInfo, res);
} else {
res.writeHead(404, `${pathInfo} was not found`);
res.end();
}
});
}
});
server.listen(8080);
console.log("listening on http://localhost:8080");