dial-a-device-node
Version:
dial-a-device-node is the device interface for dial-a-device
89 lines (72 loc) • 2.37 kB
text/coffeescript
x = require '../helper/XMLHttpRequest.js'
ajax = require '../helper/ajax.js'
###
HTTP Interface for the WebSocketRails client.
###
class WebSocketRails.HttpConnection
httpFactories: -> [
-> new x.XMLHttpRequest(),
-> new ActiveXObject("Msxml2.XMLHTTP"),
-> new ActiveXObject("Msxml3.XMLHTTP"),
-> new ActiveXObject("Microsoft.XMLHTTP")
]
createXMLHttpObject: =>
xmlhttp = false
factories =
for factory in factories
try
xmlhttp = factory()
catch e
continue
break
xmlhttp
constructor: (, ) ->
=
= new x.XMLHttpRequest()
= 0
= []
.onreadystatechange =
# .addEventListener "load", , false
.open "GET", , true
.send()
parse_stream: =>
if .readyState == 3
data = .responseText.substring
= .responseText.length
data = data.replace( /\]\]\[\[/g, "],[" )
if data != ""
decoded_data = JSON.parse data
else
decoded_data = {}
.new_message decoded_data
# if .readyState == 0
# close_event = new WebSocketRails.Event(['connection_closed',{}])
# .dispatch close_event
# if .readyState == 4
# close_event = new WebSocketRails.Event(['connection_closed',{}])
# .dispatch close_event
trigger: (event) =>
if .state != 'connected'
.push event
else
.connection_id, event.serialize()
post_data: (connection_id, payload) ->
ajax
url:
type: 'POST'
data:
client_id: connection_id
data: payload
success: ->
flush_queue: (connection_id) =>
for event in
# Events queued before connecting do not have the correct
# connection_id set yet. We need to update it before dispatching.
if connection_id?
event.connection_id = .connection_id
event
= []
connectionClosed: (event) =>
close_event = new WebSocketRails.Event(['connection_closed', event])
.state = 'disconnected'
.dispatch close_event