forked from kzokm/eyetribe-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
executable file
·79 lines (69 loc) · 2.07 KB
/
server.coffee
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
#!env coffee
#
# WebSocket proxy for The Eye Tribe Tracker
# https://github.com/kzokm/eyetribe-websocket/
#
# Copyright (c) 2014 OKAMURA Kazuhide
#
# This software is released under the MIT License.
# http://opensource.org/licenses/mit-license.php
#
{full: version} = require './lib/version'
net = require 'net'
http = require 'http'
ws = require 'websocket.io'
fs = require 'fs'
SERVER_HOST = '127.0.0.1'
SERVER_PORT = 6556
TRACKER_HOST = '127.0.0.1'
TRACKER_PORT = 6555
server = http.createServer()
.listen SERVER_PORT, SERVER_HOST, ->
console.log 'LISTEN: ' + SERVER_PORT
.on 'request', (request, response)->
if request.url == '/eyetribe.js'
code = readScript "#{__dirname}/eyetribe-#{version}.js"
else if request.url == '/eyetribe.min.js'
code = readScript "#{__dirname}/eyetribe-#{version}.min.js"
if code
response.writeHead 200,
'Content-Type': 'text/javascript; charset: UTF-8'
response.write code
else
response.writeHead 404
response.end()
readScript = (file) ->
code = readFile file
code = code?.replace '$SERVER_HOST', SERVER_HOST
code = code?.replace '$SERVER_PORT', SERVER_PORT
readFile = (file) ->
if fs.existsSync file
code = fs.readFileSync file, 'utf8'
.toString()
ws.attach server
.on 'connection', (client)->
console.log 'CONNECTED: WS'
client
.on 'message', (data)->
console.log 'REQUEST: ' + data
tracker.write data
.on 'close', (data)->
console.log 'DISCONNECTED: WS'
tracker.end()
.on 'error', (error)->
console.error 'ERROR: WS: ' + error
tracker = new net.Socket()
.on 'data', (data)->
#console.log 'RESPONSE: ' + data
try
client.write data
catch e
console.error 'ERROR: WS: ' + e
.on 'close', (data)->
console.log 'CLOSED: TRACKER SERVER'
client.end()
.on 'error', (error)->
console.error 'ERROR: TRACKER SERVER: ' + error
.connect TRACKER_PORT, TRACKER_HOST, ->
console.log 'OPENED: TRACKER SERVER'
client.write 'OK'