Trouble with NIO server in android java
- Dominant language
- Dart
- Stars
- 1.1k
- Forks
- 419
- Avg merge
- 4d 14h
- Merged PRs (30d)
- 8
Description
Hi,
I try to connect the IOWebSocketChannel to a embedded server in android. I have tested the Flutter based client side against echo.websocket.org and it works as expected.
I also can connect to the local server (code below) and the local server is already accepting the connection without any error. But when I send a new message by "channel.sink.add" it never arrives at the server. Also no messages from the server arrives to the listner in Flutter.
Its probably a really simple issue I have overseen and would be happy if someone could point me into the right direction. Thank you so much...
Client (Flutter/Dart):
```
channel = IOWebSocketChannel.connect("ws://127.0.0.1:8080");
channel.sink.add("Hi\n\r");
channel.stream.listen((message) {
print(message);
});
```
Server-Side (Android Java):
...
```
Selector selector = null; // selector is open here
try {
selector = Selector.open();
// ServerSocketChannel: selectable channel for stream-oriented listening sockets
ServerSocketChannel mSocket = ServerSocketChannel.open();
InetSocketAddress mAddr = new InetSocketAddress("localhost", 8080);
// Binds the channel's socket to a local address and configures the socket to listen for connections
mSocket.bind(mAddr);
// Adjusts this channel's blocking mode.
mSocket.configureBlocking(false);
int ops = mSocket.validOps();
SelectionKey selectKy = mSocket.register(selector, ops, null);
// Infinite loop..
// Keep server running
while (true) {
Log.i("Service","Server waiting for connections");
// Selects a set of keys whose corresponding channels are ready for I/O operations
selector.select();
// token representing the registration of a SelectableChannel with a Selector
Set mKeys = selector.selectedKeys();
Iterator mIterator = mKeys.iterator();
while (mIterator.hasNext()) {
SelectionKey myKey = mIterator.next();
// Tests whether this key's channel is ready to accept a new socket connection
if (myKey.isAcceptable()) {
SocketChannel mClient = mSocket.accept();
// Adjusts this channel's blocking mode to false
mClient.configureBlocking(false);
// Operation-set bit for read operations
mClient.register(selector, SelectionKey.OP_READ);
Log.i("Service","Connection Accepted: " + mClient.getLocalAddress() + "\n");
// Tests whether this key's channel is ready for reading
} else if (myKey.isReadable()) {
SocketChannel mClient = (SocketChannel) myKey.channel();
ByteBuffer mBuffer = ByteBuffer.allocate(1024);
mClient.read(mBuffer);
String result = new String(mBuffer.array()).trim();
Log.i("Service","Message received: " + result);
}
mIterator.remove();
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
...
```
This is the debug output I get:
```
I/Service (12526): Server Socket Start
I/Service (12526): Server waiting for connections
I/flutter (12526): Server Started
I/Service (12526): Connection Accepted: /127.0.0.1:8080
I/Service (12526): Server waiting for connections
I/Service (12526): Message received: GET / HTTP/1.1
I/Service (12526): user-agent: Dart/2.8 (dart:io)
I/Service (12526): connection: Upgrade
I/Service (12526): cache-control: no-cache
I/Service (12526): accept-encoding: gzip
I/Service (12526): content-length: 0
I/Service (12526): sec-websocket-version: 13
I/Service (12526): host: 127.0.0.1:8080
I/Service (12526): sec-websocket-extensions: permessage-deflate; client_max_window_bits
I/Service (12526): sec-websocket-key: q4raanN+HlgIxIEYfOwKJQ==
I/Service (12526): upgrade: websocket
I/Service (12526): Server waiting for connections
```
Contributor guide
Research direction
Start with the Flutter IOWebSocketChannel connection and the Android Java NIO server code shown in the issue. Compare the logged HTTP upgrade request with the server's handling of WebSocket traffic; done means establishing whether the failure is in the client or the embedded server and documenting a reproducible fix.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, dart, flutter, java
- Domain
- mobile-dev, networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100