简体   繁体   中英

websocket unable to connect to android emulator

So here's the code for my very simple WebSocket code to connect to the Android Emulator ... and it hangs.

I can confirm I can telnet to the emulator fine. I can also Websocket to a web server ok, even though http gives the old "unexpect response code 200". I can also confirm that the readState is a constant zero.

I can also confirm that a connection is established when the page is connected and disconnects when it's removed.

Whilst page up:

$ netstat -a | grep 5554
  TCP    127.0.0.1:5554         eww:0                  LISTENING
  TCP    127.0.0.1:5554         eww:49516              ESTABLISHED
  TCP    127.0.0.1:5554         eww:54424              ESTABLISHED
  TCP    127.0.0.1:49516        eww:5554               ESTABLISHED
  TCP    127.0.0.1:54424        eww:5554               ESTABLISHED

After page removed:

$ netstat -a | grep 5554
  TCP    127.0.0.1:5554         eww:0                  LISTENING
  TCP    127.0.0.1:5554         eww:49516              ESTABLISHED
  TCP    127.0.0.1:49516        eww:5554               ESTABLISHED
  TCP    127.0.0.1:54424        eww:5554               TIME_WAIT

I'm using Chrome and this is all running on Windows 7. The other connection is likely to be Eclipse.

Any thoughts?

<!DOCTYPE html>
<html>
<head>
<title>Telnet to Android Emulator</title>
<style>
    #messages {
        list-style: none;
    }
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>

<body>
<h1>Telnet to Android Emulator</h1>

<ul id="messages">
</ul>

<script>
    $(document).ready(function() {

        function log(message) {
            $('#messages').append('<li>' + message + '</li>');
        }

        var socket;

        if ("WebSocket" in window) {
            log("WebSocket API supported");
        } else {
            log("WebSocket API not supported");
        }

        function wsOpen(e) {
            log("OPEN");
        }

        function wsClose(e) {
            log("CLOSED");
        }

        function wsError(e) {
            log("ERROR " + e.data);
        }

        function wsMessage(e) {
            log(e.data);
        }

        function openSocket() {

            var wsuri = "ws://localhost:5554";

            log("connecting to " + wsuri);

            try {   
                socket = new WebSocket(wsuri);

                socket.onopen = wsOpen;
                socket.onclose = wsClose;
                socket.onerror = wsError;
                socket.onmessage = wsMessage;

            } catch (exception) {
                log('Caught ' + exception);
            }               
        }

        function closeSocket() {

            log("disconnecting");
            socket.close();
        }

        openSocket(); // down here for testing
    });
</script>
</body>
</html>

WebSocket is a protocol in its own right and the Android Emulator does not speak it. That's why it's not connecting or calling the onopen function.

Time for a new strategy

I solved the problem by writing a Java Applet which does a telnet connection then passed information from the web page through it. In this case, I used clicks on a Google Map to pass "geo fix" data.

https://github.com/stevemarvell/GoogleMapToAndroidEmulator

It's not perfect, but it's a start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM