socket2me 0.5.1 → 0.5.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f123c415850bb1bb171b3927e2b50bda54ef348e
4
- data.tar.gz: 6fcc758271404f0e3ccb7e0aea791df36ac33b18
3
+ metadata.gz: 807d92d069e30d179e6e36b8d6e9b9c1ec749c2b
4
+ data.tar.gz: ae06a77b29065e8a749bb953f54a16aa5b440c31
5
5
  SHA512:
6
- metadata.gz: 654b89e9ea16c80b880c1535f24a6d6d111124d8d6d186bbdb554daf606f913f353ae12c995b429d36d0879308fd77183709d6f3e725f4e938a58f527c8ae59c
7
- data.tar.gz: 530abc92eafcee8ebba2aa4ee94b099132ef1c4b0431bd6ba4cdee1e5ae19595e0aef56445deeda7ca15406d094262a37660ae6e8ace6b83ae39dd0a3f757722
6
+ metadata.gz: 8751a7bdcde5669e49b0849fbfff96e739a107b84da42bfd645daaad78b37d5727993b4dda549231a8b55d281914d387433d6516cc3cd49fc4a4ff3b4ec59547
7
+ data.tar.gz: be38362051e7e6988857af1560b1b866626529dfd8dfd7c7704514c835783a2f3add8eb7c5e16475f59e0ce245b64a46c3a1d6eef0bd7e8200de3f583185841e
data/README.md CHANGED
@@ -45,17 +45,16 @@ Or install it yourself as:
45
45
 
46
46
  ## Usage
47
47
 
48
- A full rack example is in `example/counter.ru`. That app simply creates
48
+ A full rack example is in `example/dummy.ru`. That app simply creates
49
49
  an HTML page and provides a helper js function to display some output. The
50
- example then outputs the server time a counter every second in a background
51
- thread.
50
+ example then outputs the server time every second in a background thread.
52
51
 
53
- To run `example/counter.rb`:
52
+ To run `example/dummy.ru`:
54
53
 
55
54
  1. checkout the project: `git clone https://github.com/ddrscott/socket2me`
56
55
  2. go into the the socket2me directory: `cd socket2me`
57
56
  3. install requirements: `./bin/setup`
58
- 4. start a rack server with the example: `rackup example/counter.ru`
57
+ 4. start a rack server with the example: `rackup example/dummy.ru`
59
58
 
60
59
  ### Rack
61
60
  ```ruby
@@ -81,8 +80,8 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
81
80
 
82
81
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
82
 
84
- Spot check everything works by running `rackup example/counter.ru`. The browser
85
- should continually append a timestamp and counter below the header.
83
+ Spot check everything works by running `rackup example/dummy.ru`. The browser
84
+ should continually append a timestamp below the header.
86
85
 
87
86
  ## Contributing
88
87
 
@@ -33,10 +33,9 @@ function appendP(text) {
33
33
  def start_counter
34
34
  Thread.abort_on_exception = true
35
35
  Thread.new do
36
- counter = 0
37
36
  while true
38
37
  sleep 1
39
- Socket2me.exec_js("appendP('[#{Time.now}]: #{counter += 1}')")
38
+ Socket2me.exec_js("appendP('#{Time.now}')")
40
39
  end
41
40
  end
42
41
  end
@@ -1,5 +1,3 @@
1
- require 'json'
2
- require 'erb'
3
1
  require 'em-websocket'
4
2
 
5
3
  module Socket2me
@@ -22,53 +20,23 @@ module Socket2me
22
20
 
23
21
  return response unless headers['Content-Type'] == 'text/html'
24
22
 
25
- full_body = body.join
26
-
27
23
  # replace the last body with script
28
- full_body.gsub!(%r{(</body>)}i,"#{script_tag}\\1")
24
+ new_body = body.join.gsub(%r{(</body>)}i,"#{script_tag}\\1")
29
25
 
30
- return status, headers, [full_body]
26
+ return status, headers, [new_body]
31
27
  end
32
28
 
33
29
  # @return [String] the outer Javascript tag with WS client script
34
30
  def script_tag
35
- js = ERB.new(js_erb).result(binding)
36
- "<script>#{js}</script>"
37
- end
38
-
39
- # Adjust the javascript client code as needed.
40
- # @return [String] inner javascript contents.
41
- def js_erb
42
- <<-JS
43
- (function () {
44
- var socket = new WebSocket("<%= @ws_url %>");
45
-
46
- function log(message) {
47
- console.log.apply(console, arguments);
48
- }
49
-
50
- try {
51
- socket.onopen = function () {
52
- log("Socket Status: " + socket.readyState + " (open)");
53
- };
54
-
55
- socket.onclose = function () {
56
- log("Socket Status: " + socket.readyState + " (closed)");
57
- };
58
-
59
- socket.onmessage = function (msg) {
60
- try {
61
- // execute in an anonymous function
62
- new Function(msg.data)();
63
- } catch (msgEx){
64
- console.error("Could not execute `%s` due to %o", msg.data, msgEx);
65
- }
66
- }
67
- } catch (exception) {
68
- console.error("Error: " + exception);
31
+ <<-HTML
32
+ <script>
33
+ (function (ws) {
34
+ ws.onmessage = function (msg) {
35
+ new Function(msg.data)();
69
36
  }
70
- })();
71
- JS
37
+ })(new WebSocket("#{@ws_url}"));
38
+ </script>
39
+ HTML
72
40
  end
73
41
  end
74
42
  end
@@ -1,3 +1,3 @@
1
1
  module Socket2me
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -41,8 +41,8 @@ module Socket2me
41
41
  @thread = Thread.new do
42
42
  EM::WebSocket.start(ws_options) do |ws|
43
43
  ws.onopen do |handshake|
44
- @clients << ws
45
44
  log "onopen: #{handshake.headers}"
45
+ @clients << ws
46
46
  end
47
47
 
48
48
  ws.onclose do |event|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: socket2me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Pierce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-27 00:00:00.000000000 Z
11
+ date: 2015-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -110,7 +110,7 @@ files:
110
110
  - Rakefile
111
111
  - bin/console
112
112
  - bin/setup
113
- - example/counter.ru
113
+ - example/dummy.ru
114
114
  - lib/socket2me.rb
115
115
  - lib/socket2me/configuration.rb
116
116
  - lib/socket2me/middleware/add_script_tag.rb
@@ -142,4 +142,3 @@ signing_key:
142
142
  specification_version: 4
143
143
  summary: Execute Javascript in the browser from the server using WebSockets
144
144
  test_files: []
145
- has_rdoc: