eventd 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.md CHANGED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 Jesse A Dunlap
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -3,7 +3,7 @@ Introduction
3
3
 
4
4
  Eventd is a web socket client and server wrapper around **em-websockets**. Eventd strives to provide an event-based
5
5
  structure, which provides the basis for web-socket enabled applications. Eventd implements a Server and a Client, which
6
- both implement a base ``EventdObject``.
6
+ both inherit from ``EventdObject``.
7
7
 
8
8
 
9
9
  Installation
@@ -18,9 +18,10 @@ class EventdClient < EventdObject
18
18
  # em-websocket socket object
19
19
  attr_accessor :socket
20
20
 
21
- def initialize(socket)
21
+ def initialize(socket, server)
22
22
  super()
23
23
  @socket = socket
24
+ @server = server
24
25
  setup_socket unless @socket == nil
25
26
  end
26
27
 
@@ -34,10 +35,18 @@ class EventdClient < EventdObject
34
35
  end
35
36
 
36
37
  @socket.onmessage do |message|
37
- self.emit('message', message, true)
38
+ begin
39
+ emission = JSON.parse message
38
40
 
39
- emission = JSON.parse message
40
- self.emit emission['channel'], emission['data']
41
+ if emission['broadcast']
42
+ self.broadcast emission['channel'], emission['data']
43
+ end
44
+
45
+ self.emit emission['channel'], emission['data']
46
+ self.emit('message', message, true)
47
+ rescue Exception
48
+ self.emit('message', message, true)
49
+ end
41
50
  end
42
51
  end
43
52
 
@@ -48,5 +57,9 @@ class EventdClient < EventdObject
48
57
  emission = { 'channel' => channel, 'priority' => 'normal', 'data' => data }
49
58
  @socket.send emission.to_json
50
59
  end
51
- end
60
+ end
61
+
62
+ def broadcast(channel, data = nil)
63
+ @server.clients.each do |client| client.emit channel, data end
64
+ end
52
65
  end
@@ -21,14 +21,18 @@ class EventdServer < EventdObject
21
21
  # Callback block which is called when the server starts for threading purposed
22
22
  attr_accessor :run_callback
23
23
 
24
+ # List of active clients on the server
25
+ attr_accessor :clients
26
+
24
27
  # Initialize the EventdServer with configuration options
25
28
  #
26
29
  # == Attributes
27
30
  # * +options+ - Configuration hash which will be used as the initial EventdServer configuration
28
31
 
29
- def initialize(options)
32
+ def initialize(options = { :host => '127.0.0.1', :port => 8080 })
30
33
  super()
31
34
  @options = options
35
+ @clients = []
32
36
  end
33
37
 
34
38
  # Add to the EventdServer configuration
@@ -47,8 +51,11 @@ class EventdServer < EventdObject
47
51
  @run_callback.call
48
52
 
49
53
  EM::WebSocket.run @options do |socket|
50
- client = EventdClient.new socket
54
+ client = EventdClient.new socket, self
51
55
  self.emit 'connection', client
56
+
57
+ self.clients.push client
58
+ handle_disconnection_for client
52
59
  end
53
60
  end
54
61
  end
@@ -57,5 +64,14 @@ class EventdServer < EventdObject
57
64
 
58
65
  def run(&callback)
59
66
  @run_callback = callback
60
- end
67
+ end
68
+
69
+ private
70
+
71
+ def handle_disconnection_for(client)
72
+ client.on 'disconnect' do
73
+ @clients.delete_if do |c| c == client end
74
+ self.emit 'disconnection', client
75
+ end
76
+ end
61
77
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventd
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.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-06-30 00:00:00.000000000 Z
12
+ date: 2013-07-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: em-websocket