-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebsocket.coffee
112 lines (93 loc) · 2.94 KB
/
websocket.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
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
Base = require './base'
platform = require '../helpers/platform'
class WebSocketRuntime extends Base
constructor: (definition) ->
super definition
@connecting = false
@connection = null
@protocol = 'noflo'
@buffer = []
@container = null
getElement: ->
return @container if @container
# DOM visualization for remote runtime output
@container = document.createElement 'div'
@container.classList.add 'preview-container'
messageConsole = document.createElement 'pre'
previewImage = document.createElement 'img'
@container.appendChild previewImage
@container.appendChild messageConsole
@on 'network', (message) ->
return unless message.command is 'output'
p = message.payload
if p.type? and p.type == 'previewurl'
hasQuery = p.url.indexOf '?' != -1
separator = if hasQuery then '&' else '?'
previewImage.src = p.url + separator + 'timestamp=' + new Date().getTime()
if p.message?
encoded = p.message.replace /[\u00A0-\u99999<>\&]/gim, (i) -> "&##{i.charCodeAt(0)};"
messageConsole.innerHTML += "#{encoded}\n"
messageConsole.scrollTop = messageConsole.scrollHeight
@on 'disconnected', ->
messageConsole.innerHTML = ''
@container
isConnected: -> @connection and @connecting == false
connect: ->
return if @connection or @connecting
if @protocol
@connection = new platform.WebSocket @getAddress(), @protocol
else
@connection = new platform.WebSocket @getAddress()
@connection.addEventListener 'open', =>
@connecting = false
# Perform capability discovery
@sendRuntime 'getruntime', {}
@emit 'status',
online: true
label: 'connected'
@emit 'connected'
@flush()
, false
@connection.addEventListener 'message', @handleMessage, false
@connection.addEventListener 'error', @handleError, false
@connection.addEventListener 'close', () =>
@connection = null
@emit 'status',
online: false
label: 'disconnected'
@emit 'disconnected'
, false
@connecting = true
disconnect: ->
return unless @connection
@connecting = false
@connection.close()
send: (protocol, command, payload) ->
if @connecting
@buffer.push
protocol: protocol
command: command
payload: payload
return
return unless @connection
@connection.send JSON.stringify @_prepareMessage protocol, command, payload
handleError: (error) =>
if @protocol is 'noflo'
delete @protocol
@connecting = false
@connection = null
setTimeout =>
@connect()
, 1
return
@emit 'error', error
@connection = null
@connecting = false
handleMessage: (message) =>
msg = JSON.parse message.data
@recvMessage msg
flush: ->
for item in @buffer
@send item.protocol, item.command, item.payload
@buffer = []
module.exports = WebSocketRuntime