Hi all-
I am trying to communicate with a Julia websocket server via Java and I encounter an error stating:
IOError: stream is closed or unusable
The server receives the message but Java never receives the response, presumably because the connection is lost. Here is the code
#Julia
using JSON, HTTP
@async HTTP.WebSockets.listen("127.0.0.1", UInt16(8081)) do ws
while !eof(ws)
recieved_message = readavailable(ws)
isempty(recieved_message) ? break : nothing
data = JSON.parse(String(recieved_message))
println(data)
send_message = "hello"
jmessage = JSON.json(send_message)
write(ws, jmessage)
end
end
#Java
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import com.google.gson.Gson;
@ClientEndpoint
public class Client {
private URI uri;
private Session session = null;
public Client(String address) throws URISyntaxException, DeploymentException, IOException {
uri = new URI(address);
ContainerProvider.getWebSocketContainer().connectToServer(this, uri);
}
@OnOpen
public void processOpen(Session session) {
this.session = session;
}
@OnMessage
public void processMessage(String message) {
System.out.println(message);
}
public void sendMessage(String message) throws IOException {
Gson gson = new Gson();
String text = gson.toJson(message);
session.getBasicRemote().sendText(text);
}
}
#Java
import java.io.IOException;
import java.net.URISyntaxException;
import javax.websocket.DeploymentException;
public class ClientTest {
public static void main(String[] args) throws URISyntaxException, DeploymentException, IOException {
Client client = new Client("ws://127.0.0.1:8081");
client.sendMessage("dfkdlfjdljfd");
}
}
Here is the full error message:
dfkdlfjdljfd
┌ Error: error handling request
│ exception =
│ IOError: stream is closed or unusable
│ Stacktrace:
│ [1] check_open at ./stream.jl:323 [inlined]
│ [2] uv_write_async(::Sockets.TCPSocket, ::Ptr{UInt8}, ::UInt64, ::Task) at ./stream.jl:901
│ [3] uv_write(::Sockets.TCPSocket, ::Ptr{UInt8}, ::UInt64) at ./stream.jl:877
│ [4] unsafe_write(::Sockets.TCPSocket, ::Ptr{UInt8}, ::UInt64) at ./stream.jl:931
│ [5] unsafe_write at /home/dfish/.julia/packages/HTTP/lZVI1/src/ConnectionPool.jl:134 [inlined]
│ [6] unsafe_write at ./io.jl:522 [inlined]
│ [7] macro expansion at ./gcutils.jl:87 [inlined]
│ [8] write at ./io.jl:545 [inlined]
│ [9] #closewrite#9(::Nothing, ::typeof(closewrite), ::HTTP.WebSockets.WebSocket{HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}) at /home/dfish/.julia/packages/HTTP/lZVI1/src/WebSockets.jl:174
│ [10] #closewrite at ./none:0 [inlined]
│ [11] #close#10(::Nothing, ::typeof(close), ::HTTP.WebSockets.WebSocket{HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}) at /home/dfish/.julia/packages/HTTP/lZVI1/src/WebSockets.jl:221
│ [12] close at /home/dfish/.julia/packages/HTTP/lZVI1/src/WebSockets.jl:220 [inlined]
│ [13] #upgrade#8(::Bool, ::typeof(HTTP.WebSockets.upgrade), ::getfield(Main, Symbol("##12#14")), ::HTTP.Streams.Stream{HTTP.Messages.Request,HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}) at /home/dfish/.julia/packages/HTTP/lZVI1/src/WebSockets.jl:148
│ [14] #upgrade at ./tuple.jl:0 [inlined]
│ [15] #6 at /home/dfish/.julia/packages/HTTP/lZVI1/src/WebSockets.jl:123 [inlined]
│ [16] macro expansion at /home/dfish/.julia/packages/HTTP/lZVI1/src/Servers.jl:360 [inlined]
│ [17] (::getfield(HTTP.Servers, Symbol("##13#14")){getfield(HTTP.WebSockets, Symbol("##6#7")){Bool,getfield(Main, Symbol("##12#14"))},HTTP.ConnectionPool.Transaction{Sockets.TCPSocket},HTTP.Streams.Stream{HTTP.Messages.Request,HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}})() at ./task.jl:268
└ @ HTTP.Servers ~/.julia/packages/HTTP/lZVI1/src/Servers.jl:364
Version Info:
Julia 1.2.0
(v1.2) pkg> st HTTP
Status `~/.julia/environments/v1.2/Project.toml`
[cd3eb016] HTTP v0.8.8