75 lines
1.1 KiB
Crystal
75 lines
1.1 KiB
Crystal
require "http/web_socket"
|
|
|
|
require "../src/colors"
|
|
require "../src/utils"
|
|
|
|
require "ipc"
|
|
|
|
require "../src/lib_modifications.cr"
|
|
|
|
require "json"
|
|
|
|
class TestIPC
|
|
property ipcc : IPC::Connection
|
|
property is_json : Bool
|
|
|
|
def initialize(service_name : String)
|
|
@is_json = uri.ends_with? ".JSON"
|
|
@ipcc = IPC::Connection.new service_name
|
|
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
|
|
m = IPC::Message.new(1.to_u8, type.to_u8, data).to_json
|
|
else
|
|
m = IPC::Message.new(1.to_u8, type.to_u8, data).to_packet
|
|
end
|
|
|
|
@ws.send m
|
|
end
|
|
|
|
def close
|
|
@ws.close
|
|
end
|
|
end
|