7

EDIT: For the solution, scroll down to the end of this post!

I have built a Socket IO + node.js chat application which works seamlessly using a web client. I can use the external IPv4 IP address to access the client across different machines on the same network. However, using socket.io-client-java in my Wear OS application is giving me issues. Specifically, an xhr poll error, every time I try to connect to the server.

I have made sure that my server is running, I have enabled android:usesCleartextTraffic="true" in AndroidManifest, and have tried changing a variety of options for my socket. I have tried several stackoverflow solutions regarding this issue, to no avail unfortunately.

Below is my index.js code, which is my server.

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

/**
 * Initializes app to be a function handler that can supply to
 * an HTTP server: in this case, index.html is served
 */
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

/**
 * Initialize a new instance of socket.io by passing
 * the server object. Listen on the 'connection' event
 * for incoming sockets and log to the console.
 */
io.on('connection', (socket) => {
  console.log('a user connected');
  console.log(socket.handshake.query);
  console.log(socket.handshake.headers);
  console.log(socket.handshake.auth.token);
  io.emit('chat message','user connected');
  socket.on('disconnect', () => {
    console.log('user disconnected');

    // Let connected users know that a user disconnected
    io.emit('chat message','user disconnected');
  });
});

/**
 * This will emit the event to all connected sockets.
 * In this case the event is receiving a chat message.
 */
 io.on('connection', (socket) => {
  socket.on('chat message', (msg) => {
    io.emit('chat message', msg);
  });
});

/**
 * This tells the server to listen on port 3000
 */
server.listen(3000, () => {
  console.log('listening on *:3000');
});

I am creating my client socket as such in MainActivity.java:

    private Socket mSocket;
{
    URI uri = URI.create("http://LOCAL_IP_ADDRESS:3000");
    IO.Options options = IO.Options.builder()
            .setUpgrade(false)
            .build();
    mSocket = IO.socket(uri, options);
    Log.d("mSocket created: ", "true");
}

The IP address is correct as I can access the web client from another device using that URL. What is strange, is that the Android client works when using an ng rok redirect. So I know it's possible, but I have no idea what I'm missing.

I apologize if anything is unclear, I have spent countless hours trying to get this working without ng rok because of latency issues, and am quite stuck now!

EDIT: I have also been able to confirm that this is only not working on the Android client. I am using the chat application to communicate between a Wear OS smart watch and an Android enabled VR headset (Pico Neo 3 Pro Eye). I am using a C# wrapper for the VR application, and this is able to connect using the above URL (http://LOCAL_IP_ADDRESS:3000). The only thing I'm doing differently is specifying the transport mode (which I have tried in the Wear OS client):

// From my Unity Application which works fine
var uri = new Uri("http://192.168.88.140:3000/");
        socket = new SocketIOUnity(uri, new SocketIOOptions
        {
            Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
        });

SOLUTION EDIT: So after literally spending 30~40 hours on this issue, I finally figured out what was going wrong. The watch I was using, the Moto 360, could connect to Wi-Fi using its standalone Wi-Fi, or over the bluetooth connection to an Android phone (i.e., using the phone's Wi-Fi connection). After all this time, I realized that the Wi-Fi connection over bluetooth was being proxied, and so was unable to connect to a locally deployed server.

Disconnecting the bluetooth on the Moto 360 and forcing it to connect to the same Wi-Fi as my server, solved the XHR Poll Error!!

0 Answers0