Skip to content

Commit

Permalink
Merge pull request #145 from piever/pv/ip
Browse files Browse the repository at this point in the history
allow IPAddr in serve
  • Loading branch information
hustf authored Mar 23, 2019
2 parents 7c2447e + e7f55b9 commit b160e60
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/HTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ function ServerWS(h::Function, w::Function; kwargs...)
ServerWS(RequestHandlerFunction(h), WSHandlerFunction(w); kwargs...)
end

to_ipaddr(ip::IPAddr) = ip
to_ipaddr(s::AbstractString) = parse(IPAddr, s)

"""
WebSockets.serve(server::ServerWS, port)
WebSockets.serve(server::ServerWS, host, port)
Expand All @@ -347,7 +350,7 @@ After a suspected connection task failure:
"""
function serve(serverws::ServerWS, host, port, verbose)
# An internal reference used for closing.
tcpserver = Ref{Union{IOServer, Nothing}}(Sockets.listen(InetAddr(parse(IPAddr, host), port)))
tcpserver = Ref{Union{IOServer, Nothing}}(Sockets.listen(InetAddr(to_ipaddr(host), port)))
# Start a couroutine that sleeps until something is put on the .in channel.
@async begin
# Next line will hold until something is put on the channel
Expand Down
9 changes: 8 additions & 1 deletion test/client_serverWS_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ end
@info "External server http request"
@test 200 == WebSockets.HTTP.request("GET", EXTERNALHTTP).status

@info "String to IPAddr conversion"
let
ip = parse(IPAddr, SURL)
@test WebSockets.to_ipaddr(SURL) == WebSockets.to_ipaddr(ip) == ip
end

@info "ServerWS: Open, http response, close. Repeat three times. Takes a while."
for i = 1:3
let
server = startserver()
ip = parse(IPAddr, SURL)
server = startserver(url=ip)
@test 200 == WebSockets.HTTP.request("GET", "http://$SURL:$PORT").status
close(server)
end
Expand Down

0 comments on commit b160e60

Please sign in to comment.