0

我正在尝试将浏览器中的 WebSocket 连接到基于 Titanium 的 iOS 应用程序上的侦听 Socket 服务器。

设备和浏览器机器在同一个无线路由器上,但​​我只能得到

[16:01:09.282] Firefox 无法与位于 ws://192.168.0.190:8080/ 的服务器建立连接。

这与协议“ws://”有关吗?Titanium Socket 侦听器如何知道期望该协议?

这是我的钛插座代码:

var hostname = Ti.Platform.address;

//Create a socket and listen for incoming connections
var listenSocket = Ti.Network.Socket.createTCP({
    host : hostname,
    port : 8080,
    accepted : function(e) {
        // This where you would usually store the newly-connected socket, e.inbound
        // so it can be used for read / write operations elsewhere in the app.
        // In this case, we simply send a message then close the socket.
        Ti.API.info("Listening socket <" + e.socket + "> accepted incoming connection <" + JSON.stringify(e.inbound) + ">");
        e.inbound.write(Ti.createBuffer({
            value : 'Hi from iOS.\r\n'
        }));
//        e.inbound.close();
        // close the accepted socket

    },
    error : function(e) {
        Ti.API.error("Socket <" + e.socket + "> encountered error when listening");
        Ti.API.error(" error code <" + e.errorCode + ">");
        Ti.API.error(" error description <" + e.error + ">");
    }
});
// Starts the socket listening for connections, does not accept them
listenSocket.listen();
Ti.API.info("Listening now...");

// Tells socket to accept the next inbound connection. listenSocket.accepted gets
// called when a connection is accepted via accept()
Ti.API.info("Calling accept.");
listenSocket.accept({
    timeout : 10000
});

这是浏览器中的代码:

function sensorClient(host, port) {
    if ("WebSocket" in window) {
        alert("WebSocket is supported by your Browser!");
        // Let us open a web socket
        var ws = new WebSocket("ws://" + host + ":" + port);
        ws.onopen = function () {
            // Web Socket is connected, send data using send()
            ws.send("hi from the browser");
        };
        ws.onmessage = function (evt) {
            var received_msg = evt.data;
            alert("Message received..." + received_msg);
        };
        ws.onclose = function () {
            // websocket is closed.
            alert("Connection is closed...");
        };
    }
    else {
        // The browser doesn't support WebSocket
        alert("WebSocket NOT supported by your Browser!");
    }
}
4

0 回答 0