|
| 1 | +# Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com> |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +require 'websocket/driver' |
| 22 | +require 'json' |
| 23 | + |
| 24 | +module Async |
| 25 | + module WebSocket |
| 26 | + # This is a basic synchronous websocket client: |
| 27 | + class Connection |
| 28 | + EVENTS = [:open, :message, :close] |
| 29 | + |
| 30 | + def initialize(socket, driver) |
| 31 | + @socket = socket |
| 32 | + @driver = driver |
| 33 | + |
| 34 | + @queue = [] |
| 35 | + |
| 36 | + @driver.on(:error) do |error| |
| 37 | + raise error |
| 38 | + end |
| 39 | + |
| 40 | + EVENTS.each do |event| |
| 41 | + @driver.on(event) do |data| |
| 42 | + @queue.push(data) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + @driver.start |
| 47 | + end |
| 48 | + |
| 49 | + attr :driver |
| 50 | + attr :url |
| 51 | + |
| 52 | + def next_event |
| 53 | + while @queue.empty? |
| 54 | + data = @socket.read(1024) |
| 55 | + |
| 56 | + if data and !data.empty? |
| 57 | + @driver.parse(data) |
| 58 | + else |
| 59 | + return nil |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + @queue.shift |
| 64 | + rescue EOFError |
| 65 | + return nil |
| 66 | + end |
| 67 | + |
| 68 | + def next_message |
| 69 | + while event = next_event |
| 70 | + if event.is_a? ::WebSocket::Driver::MessageEvent |
| 71 | + return JSON.parse(event.data) |
| 72 | + elsif event.is_a? ::WebSocket::Driver::CloseEvent |
| 73 | + return nil |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def send_message(message) |
| 79 | + @driver.text(JSON.dump(message)) |
| 80 | + end |
| 81 | + |
| 82 | + def write(data) |
| 83 | + @socket.write(data) |
| 84 | + end |
| 85 | + |
| 86 | + def close |
| 87 | + @driver.close |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | +end |
0 commit comments