-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.coffee
More file actions
160 lines (116 loc) · 3.38 KB
/
common.coffee
File metadata and controls
160 lines (116 loc) · 3.38 KB
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
158
w3cwebsocket = (require "websocket").w3cwebsocket
exports.ZeroWebsocket =
class ZeroWebsocket
constructor: (url) ->
@url = url
@next_message_id = 1
@waiting_cb = {}
connect: ->
@ws = new w3cwebsocket(@url,null,null,null,null,{
maxReceivedFrameSize: 50*1024*1024
})
@ws.onmessage = @onMessage
@ws.onopen = @onOpenWebsocket
@ws.onerror = @onErrorWebsocket
@ws.onclose = @onCloseWebsocket
onMessage: (e) =>
message = JSON.parse(e.data)
cmd = message.cmd
if cmd == "response"
if @waiting_cb[message.to]?
@waiting_cb[message.to](message.result)
else
@log "Websocket callback not found:", message
else if cmd == "ping"
@response message.id, "pong"
else
@route cmd, message
route: (cmd, message) =>
@log "Unknown command", message
response: (to, result) ->
@send {"cmd": "response", "to": to, "result": result}
cmd: (cmd, params={}, cb=null) ->
@send {"cmd": cmd, "params": params}, cb
send: (message, cb=null) ->
if not message.id?
message.id = @next_message_id
@next_message_id += 1
@ws.send(JSON.stringify(message))
if cb
@waiting_cb[message.id] = cb
log: (args...) =>
console.log "[ZeroWebsocket]", args...
onOpenWebsocket: (e) =>
@log "Open"
console.log "open"
if @onOpen? then @onOpen(e)
onErrorWebsocket: (e) =>
@log "Error", e
if @onError? then @onError(e)
onCloseWebsocket: (e, reconnect=10000) =>
@log "Closed", e
if e and e.code == 1000 and e.wasClean == false
@log "Server error, please reload the page", e.wasClean
else # Connection error
setTimeout (=>
@log "Reconnecting..."
@connect()
), reconnect
if @onClose? then @onClose(e)
toGMT8Sec:(t)=>
if typeof t is 'number'
return t + 8*60*60
else
return @toGMT8Sec t.getTime()/1000
toGMT8MSec: (t)=>
if typeof t is 'number'
return t + 8*60*60*1000
else
return @toGMT8MSec t.getTime()
http = require 'http'
https = require 'https'
exports.Parse=
class Parse
constructor: (option,createFunc) ->
self=@
@createFunc = createFunc
if !option.host
option.host = "127.0.0.1"
if !option.port
option.port= 43110
if !option.proto
option.proto={http:"http",ws:"ws"}
if !option.proto.ws
option.proto.ws="ws"
if !option.proto.http
option.proto.ws="http"
@option = option
if option.proto.http is "http"
mod=http
else
mod=https
console.log "geting:
#{@option.proto.http}://#{@option.host}:#{@option.port}/#{@option.addr}"
mod.get {
hostname:option.host
port:option.port
path:'/'+option.addr
}, (res)->
if res.error
console.log "error",res.error
return
body=[]
res.on 'data', (chunk)->
body.push chunk
res.on 'end', ()->
self.parse(body)
parse: (bodies)=>
str=Buffer.concat(bodies).toString()
@wrapper_nonce = (str.match /wrapper_nonce.*"(.*)"/)[1]
@wrapper_key = (str.match /wrapper_key.*"(.*)"/)[1]
console.log "wrapper_nonce:",@wrapper_nonce
console.log "wrapper_key:",@wrapper_key
url = @option.proto.ws+"://"+@option.host+":"+@option.port+"/Websocket?wrapper_key="+@wrapper_key
@zws = @createFunc url
console.log "connect to ",url
@zws.connect()