将WebSocket连接到侦听Titanium Ti.Network.Socket.TCP?

时间:2013-03-28 21:07:05

标签: websocket titanium

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

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

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

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

这是我的Titanium套接字代码:

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!");
    }
}

0 个答案:

没有答案