3
0
Fork 0
websocketd/tests/test-ws.cr

75 lines
1.1 KiB
Crystal
Raw Normal View History

2020-07-04 22:31:17 +02:00
require "http/web_socket"
require "../src/colors"
require "../src/utils"
require "ipc"
require "../src/lib_modifications.cr"
require "json"
class TestIPC
2020-07-23 01:08:36 +02:00
property ipcc : IPC::Client
2020-07-04 22:31:17 +02:00
property is_json : Bool
def initialize(service_name : String)
@is_json = uri.ends_with? ".JSON"
2020-07-23 01:08:36 +02:00
@ipcc = IPC::Client.new service_name
2020-07-04 22:31:17 +02:00
end
# TODO
#def run
# yield @ipcc
# ipcc.close
#end
end
class TestWS
property ws : WebSocket
property is_json : Bool
def initialize(uri : String)
@ws = WebSocket.new(URI.parse(uri))
@is_json = uri.ends_with? ".JSON"
@ws.on_close do |socket|
raise "socket is closing"
end
end
def read
m = nil
loop do
m = @ws.run_once
if m.nil?
raise "empty message"
end
# remove ping messages, they are not application-relevent
unless m.is_a?(HTTP::WebSocket::Ping)
break
end
puts "received a ping message, skipping"
end
m
end
def send(type : Int32, data : String)
m : String | Bytes
if @is_json
2020-07-23 01:08:36 +02:00
m = IPC::Message.new(0, 1.to_u8, type.to_u8, data).to_json
2020-07-04 22:31:17 +02:00
else
2020-07-23 01:08:36 +02:00
m = IPC::Message.new(0, 1.to_u8, type.to_u8, data).to_packet
2020-07-04 22:31:17 +02:00
end
@ws.send m
end
def close
@ws.close
end
end