开发者

Send string to a tcp socket using GapSocket

开发者 https://www.devze.com 2023-04-12 11:16 出处:网络
I am trying to establish a socket connection using GapSocket and send some data (strings) to a tcp socket from an PhoneGap to a computer with port 8888 open. I have included all dependencies:

I am trying to establish a socket connection using GapSocket and send some data (strings) to a tcp socket from an PhoneGap to a computer with port 8888 open. I have included all dependencies:

  1. Both asyncsocket.m and asynsocket.h from cocoaasyncsocket
  2. Both GapSocketCommand.m and GapSocketCommand.h from GapSocket
  3. Included GapSocket.js and referenced the js file from index.html under www folder.

Following is my index.html file:

<!DOCTYPE html>
<html>
<head>
    <title>Socket Test</title>
    <script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
    <script type="text/javascript">
        document.addEventListener("deviceready", function(){
            var mySocket = new GapSocket(127.0.0.1, 8888);
            mySocket.onopen = function(){ alert("Socket open开发者_Go百科ed."); };
            mySocket.send("some data here");
        }, false);
     </script>
    </head>
<body>
</body>

It compiles okay and doesn't throw any dependency error and able to run on iOS Simulator. Before I run, I opened port 8888 on 127.0.0.1 (the machine that the Simulator runs) using:

nc -l 127.0.0.1 8888

I can connect the open port and send data by using telnet:

telnet 127.0.0.1 8888

with the following data send:

eddy-2:~ eddy$ telnet 127.0.0.1 8888
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
test
test

Coming back to the iOS Simulator, it runs but doesn't send anything out and I am not sure if I am doing the correct way of initializing the socket (I am new to both PhoneGap and Xcode). I followed steps on the readme but it doesn't provide much context.


you need to include an entry in cordova.plist plugin with GapSocketCommand string and value


Two things are wrong:

  • First, you need to put the IP address in quotes.
  • Second, you need to use the IP address of your machine running netcat (127.0.0.1 is the IP address of the simulator itself; you should be able to find out your IP address using ifconfig or similar).

As an example, it should look something like this:

var mySocket = new GapSocket("192.168.0.100", 8888);

Also, don't forget to register the plugin in PhoneGap.plist.

0

精彩评论

暂无评论...
验证码 换一张
取 消