webmate-client 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,25 +33,6 @@ Backbone.Collection::bindSocketEvents = () ->
|
|
33
33
|
if collection.remove(collection.parse(response))
|
34
34
|
collection.trigger('change', collection, response, {})
|
35
35
|
|
36
|
-
###
|
37
|
-
# bind
|
38
|
-
client.on "#{path}/update", (response, params) =>
|
39
|
-
return unless response._id
|
40
|
-
@get(response._id).set(response)
|
41
|
-
|
42
|
-
client.on "#{@collectionName()}/read", (response, params)->
|
43
|
-
model.reset(response)
|
44
|
-
model.trigger "sync", model, response
|
45
|
-
|
46
|
-
client.on "#{@collectionName()}/create", (response, params)->
|
47
|
-
model.add(response)
|
48
|
-
#model.get(response._id).trigger 'sync', response
|
49
|
-
#if clientId is params._client_id
|
50
|
-
# model.add(response)
|
51
|
-
#else
|
52
|
-
# model.get(params._cid).set(response)
|
53
|
-
###
|
54
|
-
|
55
36
|
# update existing functions
|
56
37
|
|
57
38
|
Backbone.Collection::_prepareModelWithoutAssociations = Backbone.Collection::_prepareModel
|
@@ -1,43 +1,39 @@
|
|
1
1
|
class Webmate.Client
|
2
2
|
constructor: (channel_name) ->
|
3
|
-
self = @
|
4
3
|
@bindings = {}
|
5
|
-
@channel_name = channel_name
|
4
|
+
@channel_name = channel_name || 'http_over_websocket'
|
6
5
|
|
7
6
|
if @useWebsockets()
|
8
7
|
@websocket = @createConnection( (message) ->
|
8
|
+
console.log(message)
|
9
9
|
metadata = message.request.metadata
|
10
10
|
eventBindings = @bindings["#{metadata.collection_url}/#{metadata.method}"]
|
11
|
-
|
11
|
+
|
12
12
|
_.each eventBindings, (eventBinding) ->
|
13
13
|
eventBinding(message.response.body, message.request.metadata)
|
14
14
|
)
|
15
|
-
|
15
|
+
|
16
16
|
useWebsockets: ->
|
17
17
|
window.Webmate.websocketsEnabled isnt false && io && io.Socket
|
18
18
|
|
19
19
|
createConnection: (onMessageHandler) ->
|
20
|
-
self = @
|
21
|
-
@clientId or= Math.random().toString(36).substr(2)
|
22
|
-
|
23
20
|
# pass callback func, if needed be sure what callback exists
|
24
|
-
token = Webmate.Auth.getToken()
|
25
|
-
return false unless token?
|
26
|
-
|
21
|
+
# token = Webmate.Auth.getToken()
|
22
|
+
# return false unless token?
|
23
|
+
token = false
|
27
24
|
socket = new io.Socket
|
28
25
|
resource: @channel_name
|
29
26
|
host: location.hostname
|
30
27
|
port: Webmate.websocketsPort or location.port
|
31
|
-
query: $.param(token: token)
|
28
|
+
query: if token then $.param(token: token) else ''
|
32
29
|
|
33
30
|
socket.on "connect", () ->
|
34
31
|
console.log("connection established")
|
35
32
|
|
36
|
-
socket.onPacket = (packet)
|
37
|
-
console.log(packet)
|
33
|
+
socket.onPacket = (packet) =>
|
38
34
|
return unless packet.type is 'message'
|
39
|
-
|
40
|
-
onMessageHandler.call(
|
35
|
+
response = JSON.parse(packet.data)
|
36
|
+
onMessageHandler.call(@, response)
|
41
37
|
|
42
38
|
socket.connect()
|
43
39
|
socket
|
@@ -47,87 +43,21 @@ class Webmate.Client
|
|
47
43
|
@bindings[action].push(callback)
|
48
44
|
@
|
49
45
|
|
50
|
-
send: (path,
|
46
|
+
send: (path, params, method) ->
|
47
|
+
data = {}
|
51
48
|
data.path = path
|
52
49
|
data.method = method
|
50
|
+
data.params = params
|
51
|
+
data.params.metadata = {
|
52
|
+
request_id: Math.random().toString(36).substr(2);
|
53
|
+
}
|
53
54
|
packet = {
|
54
55
|
type: 'message',
|
55
56
|
data: JSON.stringify(data)
|
56
57
|
}
|
57
58
|
@websocket.packet(packet)
|
58
59
|
|
59
|
-
Webmate.Client::parsePacketData = (packet_data) ->
|
60
|
-
data = JSON.parse(packet_data)
|
61
|
-
data.response.body = JSON.parse(data.response.body)
|
62
|
-
data
|
63
|
-
|
64
|
-
Webmate.connect = (channel, callback)->
|
65
|
-
client = new Webmate.Client(channel, callback)
|
66
|
-
Webmate.channels[channel] = client
|
67
|
-
client
|
68
|
-
|
69
|
-
###
|
70
|
-
class Webmate.Client
|
71
|
-
getFullPath: ->
|
72
|
-
"#{location.hostname}:#{Webmate.websocketsPort or location.port}/#{@channel}"
|
73
|
-
|
74
|
-
getClientId: ->
|
75
|
-
@clientId or= Math.random().toString(36).substr(2)
|
76
|
-
|
77
|
-
buildSocket: (onMessageHandler) ->
|
78
|
-
|
79
|
-
constructor: (channel, callback) ->
|
80
|
-
self = @
|
81
|
-
@bindings = {}
|
82
|
-
@channel = channel
|
83
|
-
|
84
|
-
if window.Webmate.websocketsEnabled isnt false && window.WebSocket
|
85
|
-
@websocket = buildSocket( (message) ->
|
86
|
-
)
|
87
|
-
|
88
|
-
@websocket = new WebSocket("ws://#{@fullPath}")
|
89
|
-
# prepare queue to store requests if socket not ready
|
90
|
-
@callsQueue = new Array()
|
91
|
-
@websocket.onmessage = (e) ->
|
92
|
-
data = JSON.parse(e.data)
|
93
|
-
eventBinding = self.bindings[data.action]
|
94
|
-
_.each eventBinding, (binding)->
|
95
|
-
binding(data.response, data.params)
|
96
|
-
@websocket.onopen = (e) ->
|
97
|
-
# process pending queues
|
98
|
-
while data = self.callsQueue.pop()
|
99
|
-
self.websocket.send(JSON.stringify(data))
|
100
|
-
callback() if callback
|
101
|
-
else
|
102
|
-
if window.Webmate.websocketsEnabled is false
|
103
|
-
console.log("Websockets is disabled. Using http.")
|
104
|
-
else
|
105
|
-
console.log("Websocket not supported. Using http.")
|
106
|
-
callback() if callback
|
107
|
-
@
|
108
|
-
on: (action, callback)->
|
109
|
-
@bindings[action] = [] if !@bindings[action]
|
110
|
-
@bindings[action].push(callback)
|
111
|
-
@
|
112
|
-
send: (action, data, method)->
|
113
|
-
data = {} if !data
|
114
|
-
method = 'get' if !method
|
115
|
-
data.action = action
|
116
|
-
data.channel = @channel
|
117
|
-
data._client_id = @clientId
|
118
|
-
|
119
|
-
if @websocket
|
120
|
-
if @websocket.readyState == @websocket.OPEN
|
121
|
-
@websocket.send(JSON.stringify(data))
|
122
|
-
else
|
123
|
-
@callsQueue.push(data)
|
124
|
-
else
|
125
|
-
$.ajax("http://#{@fullPath}/#{action}", type: method).success (data) ->
|
126
|
-
console.log(data)
|
127
|
-
@
|
128
|
-
|
129
60
|
Webmate.connect = (channel, callback)->
|
130
61
|
client = new Webmate.Client(channel, callback)
|
131
62
|
Webmate.channels[channel] = client
|
132
63
|
client
|
133
|
-
###
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webmate-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: coffee-script
|