Skip to content

Commit

Permalink
Under construction...
Browse files Browse the repository at this point in the history
  • Loading branch information
EricForgy committed Feb 11, 2018
1 parent ee2067c commit 495c372
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 46 deletions.
51 changes: 40 additions & 11 deletions src/WebSockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ struct WebSocketHeader
mask::UInt32
end

@enum ReadyState CONNECTED=0x1 CLOSING=0x2 CLOSED=0x3

mutable struct WebSocket{T <: IO} <: IO
io::T
frame_type::UInt8
server::Bool
rxpayload::Vector{UInt8}
txpayload::Vector{UInt8}
txclosed::Bool
rxclosed::Bool
txstate::ReadyState
rxstate::ReadyState
end

function WebSocket(io::T; server=false, binary=false) where T <: IO
WebSocket{T}(io, binary ? WS_BINARY : WS_TEXT, server,
UInt8[], UInt8[], false, false)
UInt8[], UInt8[], CONNECTED, CONNECTED)
end


Expand Down Expand Up @@ -168,12 +169,25 @@ function Base.write(ws::WebSocket, x1, x2, xs...)
end


function IOExtras.closewrite(ws::WebSocket)
@require !ws.txclosed
function closewrite(ws::WebSocket)
# println("(closewrite) server: $(ws.server), rxstate: $(ws.rxstate), txstate: $(ws.txstate)")
@require ws.txstate == CONNECTED
opcode = WS_FINAL | WS_CLOSE
@debug 1 "WebSocket ⬅️ $(WebSocketHeader(opcode, 0x00))"
write(ws.io, opcode, 0x00)
ws.txclosed = true
isopen(ws.io) && write(ws.io, opcode, 0x00)
ws.txstate = CLOSING
end


function closeread(ws::WebSocket)
# println("(closeread) server: $(ws.server), rxstate: $(ws.rxstate), txstate: $(ws.txstate)")
@require ws.rxstate == CONNECTED
opcode = WS_FINAL | WS_CLOSE
@debug 1 "WebSocket ⬅️ $(WebSocketHeader(opcode, 0x00))"
# println("(closeread) Send close opcode")
isopen(ws.io) && write(ws.io, opcode, 0x00)
# println("(closeread) Close opcode sent")
ws.rxstate = CLOSING
end


Expand Down Expand Up @@ -218,16 +232,23 @@ end


function Base.close(ws::WebSocket)
if !ws.txclosed
# println("(close) server: $(ws.server), rxstate: $(ws.rxstate), txstate: $(ws.txstate)")
# println("Check CONNECTED")
if ws.txstate == CONNECTED
closewrite(ws)
end
while !ws.rxclosed
ws.rxstate = CLOSING
# println("Check CLOSING")
while !eof(ws) && ws.rxstate == CLOSING
readframe(ws)
end
# println("(close) Close")
isopen(ws.io) && close(ws.io)
# println("(close) Closed")
end


Base.isopen(ws::WebSocket) = !ws.rxclosed
Base.isopen(ws::WebSocket) = (ws.rxstate == CONNECTED) && isopen(ws.io)



Expand Down Expand Up @@ -264,14 +285,22 @@ function readframe(ws::WebSocket)
end

if h.opcode == WS_CLOSE
ws.rxclosed = true
# println("Received a close opcode")
# println("(readframe) server: $(ws.server), rxstate: $(ws.rxstate), txstate: $(ws.txstate)")
if h.length >= 2
status = UInt16(ws.rxpayload[1]) << 8 | ws.rxpayload[2]
if status != 1000
message = String(ws.rxpayload[3:h.length])
throw(WebSocketError(status, message))
end
end
if ws.rxstate == CONNECTED
closeread(ws)
end
if ws.rxstate == CLOSING
ws.rxstate = CLOSED
isopen(ws.io) && close(ws.io)
end
return UInt8[]
elseif h.opcode == WS_PING
write(ws.io, [WS_PONG, 0x00])
Expand Down
44 changes: 22 additions & 22 deletions test/WebSockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ using HTTP.IOExtras

@testset "WebSockets" begin

for s in ["ws", "wss"]
# for s in ["ws", "wss"]

HTTP.WebSockets.open("$s://echo.websocket.org") do ws
write(ws, HTTP.bytes("Foo"))
@test !eof(ws)
@test String(readavailable(ws)) == "Foo"
# HTTP.WebSockets.open("$s://echo.websocket.org") do ws
# write(ws, HTTP.bytes("Foo"))
# @test !eof(ws)
# @test String(readavailable(ws)) == "Foo"

write(ws, HTTP.bytes("Hello"))
write(ws, " There")
write(ws, " World", "!")
closewrite(ws)
# write(ws, HTTP.bytes("Hello"))
# write(ws, " There")
# write(ws, " World", "!")
# closewrite(ws)

io = IOBuffer()
write(io, ws)
@test String(take!(io)) == "Hello There World!"
# io = IOBuffer()
# write(io, ws)
# @test String(take!(io)) == "Hello There World!"

close(ws)
end
# close(ws)
# end

end
# end

p = 8000

Expand All @@ -46,15 +46,15 @@ HTTP.WebSockets.open("ws://127.0.0.1:$(p)") do ws
@test !eof(ws)
@test String(readavailable(ws)) == "Foo"

write(ws, HTTP.bytes("Hello"))
write(ws, " There")
write(ws, " World", "!")
# write(ws, HTTP.bytes("Hello"))
# write(ws, " There")
# write(ws, " World", "!")

closewrite(ws)
# closewrite(ws)

io = IOBuffer()
write(io, ws)
@test String(take!(io)) == "Hello There World!"
# io = IOBuffer()
# write(io, ws)
# @test String(take!(io)) == "Hello There World!"

close(ws)
end
Expand Down
26 changes: 13 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
@static if VERSION >= v"0.7.0-DEV.2915"
using Distributed
end
addprocs(5)
# addprocs(5)

using HTTP
using HTTP.Dates
using HTTP.Test


@testset "HTTP" begin
include("utils.jl");
include("sniff.jl");
include("uri.jl");
include("url.jl");
include("cookies.jl");
include("parser.jl");
# include("utils.jl");
# include("sniff.jl");
# include("uri.jl");
# include("url.jl");
# include("cookies.jl");
# include("parser.jl");

include("loopback.jl");
# include("loopback.jl");
include("WebSockets.jl");
include("messages.jl");
include("client.jl");
# include("messages.jl");
# include("client.jl");

include("handlers.jl")
include("server.jl")
# include("handlers.jl")
# include("server.jl")

include("async.jl");
# include("async.jl");
end;

0 comments on commit 495c372

Please sign in to comment.