tamashii-client 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +181 -1
- data/lib/tamashii/client/base.rb +2 -1
- data/lib/tamashii/client/config.rb +3 -3
- data/lib/tamashii/client/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 616b8acc7d50fcb48f9b8e3cd333fd664e41e9cb
|
4
|
+
data.tar.gz: 0c4529a5ee7eee3844f6cad3d05052d01b656526
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e329f34d53186908f64c96143acc0ba3dcd7d657f8d85957a204bdc9c12b1cf8638a3b8611409f5fb99a16d845e22c2dbfdcaf3b0b4e8f0118bf2531700fc60
|
7
|
+
data.tar.gz: 11f3285f16c6d6634603cc1b82b8ebd60ebd9ee61e3e71ce60c1e62be832eb06f7353086a9937944763e9187eb6ca7f926d5560c453393dbbc2a95f0a839a0a8
|
data/README.md
CHANGED
@@ -1 +1,181 @@
|
|
1
|
-
|
1
|
+
---
|
2
|
+
tags: tamashii, readme
|
3
|
+
---
|
4
|
+
|
5
|
+
Tamashii Client [![Gem Version](https://badge.fury.io/rb/tamashii-client.svg)](https://badge.fury.io/rb/tamashii-client)
|
6
|
+
===
|
7
|
+
|
8
|
+
Tamashii Client is the client side of the WebSocket framework [Tamashii](https://github.com/tamashii-io/tamashii). It is event-driven and it provides high-level API for users to communicates with WebSocket server easily.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add the following code to your `Gemfile`
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```ruby
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself with:
|
20
|
+
```ruby
|
21
|
+
$ gem install tamashii-client
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
With Tamashii Client, we just need to focus on how to handle the events such as `on_connect` for connection opened or `on_message` for receiving messages from server.
|
27
|
+
|
28
|
+
### Simple Example
|
29
|
+
|
30
|
+
A simple example of using Tamashii Client follows. Here we will connect to the [wss://echo.websocket.org](wss://echo.websocket.org), the echo testing for WebSocket.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'tamashii/client'
|
34
|
+
|
35
|
+
# configuration for client. Can be seperated into other file
|
36
|
+
Tamashii::Client.config do
|
37
|
+
# whether to use TLS or not. Here we connect to 'wss', so the value is true
|
38
|
+
use_ssl true
|
39
|
+
# the hostname WITHOUT url scheme
|
40
|
+
host "echo.websocket.org"
|
41
|
+
# the port to connect with. 443 for HTTPS and WSS
|
42
|
+
# Note the current version client does not infer the port from 'use_ssl'
|
43
|
+
# So you must explictly specifiy the port to use
|
44
|
+
port 443
|
45
|
+
# the log file for internel connection log
|
46
|
+
# default is STDOUT
|
47
|
+
log_file 'tamashii.log'
|
48
|
+
end
|
49
|
+
|
50
|
+
client = Tamashii::Client::Base.new
|
51
|
+
@server_opened = false
|
52
|
+
|
53
|
+
# callback for server opened
|
54
|
+
# called when the WebSocket connection is readt
|
55
|
+
client.on(:open) do
|
56
|
+
@server_opened = true
|
57
|
+
end
|
58
|
+
# callback for receving messages
|
59
|
+
# The data received is represented in a byte array
|
60
|
+
# You may need to 'pack' it back to Ruby string
|
61
|
+
client.on(:message) do |message|
|
62
|
+
puts "Received: #{message.pack('C*')}"
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
# sending loop
|
67
|
+
# We send a request to server every second and terminates after 10 seconds
|
68
|
+
# In the begining, the server is not opened so the sending may fail.
|
69
|
+
count = 0
|
70
|
+
loop do
|
71
|
+
sleep 1
|
72
|
+
if @server_opened # can also use 'client.opened?'
|
73
|
+
client.transmit "Hello World! #{count}"
|
74
|
+
else
|
75
|
+
puts "Unable to send #{count}: server not opened"
|
76
|
+
end
|
77
|
+
count += 1
|
78
|
+
if count >= 10
|
79
|
+
client.close
|
80
|
+
break
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
The [wss://echo.websocket.org](wss://echo.websocket.org) will echo the messages back to the client. So if everything works fine, you will see following output:
|
86
|
+
|
87
|
+
```text
|
88
|
+
Unable to send 0: server not opened
|
89
|
+
Received: Hello World! 1
|
90
|
+
Received: Hello World! 2
|
91
|
+
Received: Hello World! 3
|
92
|
+
Received: Hello World! 4
|
93
|
+
Received: Hello World! 5
|
94
|
+
Received: Hello World! 6
|
95
|
+
Received: Hello World! 7
|
96
|
+
Received: Hello World! 8
|
97
|
+
Received: Hello World! 9
|
98
|
+
```
|
99
|
+
|
100
|
+
If you look into the log file (`tamashii.log` in this example), you can find the connection details of Tamashii Client.
|
101
|
+
|
102
|
+
```log
|
103
|
+
[2017-09-08 03:39:38] INFO -- WebSocket Client : Worker Create!
|
104
|
+
[2017-09-08 03:39:39] INFO -- WebSocket Client : Trying to open the socket...
|
105
|
+
[2017-09-08 03:39:40] INFO -- WebSocket Client : Socket opened!
|
106
|
+
[2017-09-08 03:39:40] INFO -- WebSocket Client : WebSocket Server opened
|
107
|
+
[2017-09-08 03:39:41] DEBUG -- WebSocket Client : Message from server: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 49]
|
108
|
+
[2017-09-08 03:39:41] DEBUG -- WebSocket Client : Message from server: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 50]
|
109
|
+
[2017-09-08 03:39:42] DEBUG -- WebSocket Client : Message from server: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 51]
|
110
|
+
[2017-09-08 03:39:43] DEBUG -- WebSocket Client : Message from server: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 52]
|
111
|
+
[2017-09-08 03:39:44] DEBUG -- WebSocket Client : Message from server: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 53]
|
112
|
+
[2017-09-08 03:39:45] DEBUG -- WebSocket Client : Message from server: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 54]
|
113
|
+
[2017-09-08 03:39:46] DEBUG -- WebSocket Client : Message from server: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 55]
|
114
|
+
[2017-09-08 03:39:47] DEBUG -- WebSocket Client : Message from server: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 56]
|
115
|
+
[2017-09-08 03:39:48] DEBUG -- WebSocket Client : Message from server: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 57]
|
116
|
+
[2017-09-08 03:39:49] INFO -- WebSocket Client : WebSocket Server closed
|
117
|
+
[2017-09-08 03:39:49] INFO -- WebSocket Client : Socket closed
|
118
|
+
[2017-09-08 03:39:49] DEBUG -- WebSocket Client : Worker terminales normally
|
119
|
+
```
|
120
|
+
|
121
|
+
The log level can be changed in the configuration using `log_level`. For example, to change to level to `INFO`:
|
122
|
+
```ruby
|
123
|
+
Tamashii::Client.config do
|
124
|
+
log_level :INFO
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
### The events and callbacks
|
129
|
+
|
130
|
+
These are events in the Tamashii Client. You can use `on` method to register callbacks for them.
|
131
|
+
- `socket_opened`
|
132
|
+
- When the low-level io socket (`TCPSocket` or `OpenSSL::SSL::SSLSocket`) successfully connected to the server.
|
133
|
+
- Receving this event does not imply the server supports WebSocket.
|
134
|
+
- `open`
|
135
|
+
- When the WebSocket handshake is finished and the connection is opened
|
136
|
+
- Client can start sending data to server after receiving this event.
|
137
|
+
- `message`
|
138
|
+
- When the client receives the WebSocket payload from server.
|
139
|
+
- The message payload will be pass as the argument of the callback.
|
140
|
+
- `error`
|
141
|
+
- When there is a protocol error due to bad data sent by the other peer.
|
142
|
+
- This event is purely informational, you do not need to implement error recovery.
|
143
|
+
- The error object will be pass as the argument of the callback.
|
144
|
+
- `close`
|
145
|
+
- When the WebSocket is closed **normally**.
|
146
|
+
- Will **NOT** be fired when the connection is closed by low-level IO error such as connection reset.
|
147
|
+
- `socket_closed`
|
148
|
+
- When the low-level socket is closed.
|
149
|
+
- Will be fired no matter the WebSocket is closed normally or not.
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
### Cooperate with Tamashii Server
|
154
|
+
|
155
|
+
Above example using the [wss://echo.websocket.org](wss://echo.websocket.org) to test your client. You can also use the [Tamashii](https://github.com/tamashii-io/tamashii) server to test your client. Only thing to do is to change the `host` and `port` in the configuration into the one used by your Tamashii server.
|
156
|
+
|
157
|
+
## Development
|
158
|
+
|
159
|
+
To get the source code
|
160
|
+
|
161
|
+
$ git clone git@github.com:tamashii-io/tamashii-client.git
|
162
|
+
|
163
|
+
Initialize the development environment
|
164
|
+
|
165
|
+
$ ./bin/setup
|
166
|
+
|
167
|
+
Run the spec
|
168
|
+
|
169
|
+
$ rspec
|
170
|
+
|
171
|
+
Installation the version of development on localhost
|
172
|
+
|
173
|
+
$ bundle exec rake install
|
174
|
+
|
175
|
+
## Contribution
|
176
|
+
|
177
|
+
Please report to us on [Github](https://github.com/tamashii-io/tamashii-client) if there is any bug or suggested modified.
|
178
|
+
|
179
|
+
The project was developed by [5xruby Inc.](https://5xruby.tw/)
|
180
|
+
|
181
|
+
|
data/lib/tamashii/client/base.rb
CHANGED
@@ -20,7 +20,8 @@ module Tamashii
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def initialize
|
23
|
-
|
23
|
+
entry_point_with_slash = Config.entry_point.start_with?("/") ? Config.entry_point : "/#{Config.entry_point}"
|
24
|
+
@url = "#{Config.use_ssl ? "wss" : "ws"}://#{Config.host}:#{Config.port}#{entry_point_with_slash}"
|
24
25
|
|
25
26
|
@callbacks = {}
|
26
27
|
|
@@ -5,12 +5,12 @@ module Tamashii
|
|
5
5
|
register :log_file, STDOUT
|
6
6
|
|
7
7
|
register :use_ssl, false
|
8
|
-
register :entry_point, "
|
8
|
+
register :entry_point, ""
|
9
9
|
register :host, "localhost"
|
10
10
|
register :port, 3000
|
11
|
-
register :opening_timeout,
|
11
|
+
register :opening_timeout, 10
|
12
12
|
register :opening_retry_interval, 1
|
13
|
-
register :closing_timeout,
|
13
|
+
register :closing_timeout, 10
|
14
14
|
|
15
15
|
def log_level(level = nil)
|
16
16
|
return Client.logger.level if level.nil?
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tamashii-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 蒼時弦也
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-09-
|
13
|
+
date: 2017-09-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: websocket-driver
|