Send string to a tcp socket using GapSocket

最后都变了- 提交于 2020-01-17 01:13:11

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/7747848/send-string-to-a-tcp-socket-using-gapsocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!