使用 GapSocket 将字符串发送到 tcp 套接字
我正在尝试使用 GapSocket 建立套接字连接并发送一些将数据(字符串)从 PhoneGap 传输到打开端口 8888 的计算机的 TCP 套接字。我已包含所有依赖项:
- 来自 cocoaasyncsocket 的 asyncsocket.m 和 asyncsocket.h 均是
- GapSocketCommand.m和 GapSocket 中的 GapSocketCommand.h
- 包含 GapSocket.js 并引用 www 文件夹下的 index.html 中的 js 文件。
以下是我的index.html 文件:
<!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>
它编译正常,不会引发任何依赖错误,并且能够在 iOS 模拟器上运行。在运行之前,我使用以下命令打开了 127.0.0.1(模拟器运行的计算机)上的端口 8888:
nc -l 127.0.0.1 8888
我可以使用 telnet 连接开放端口并发送数据:
远程登录127.0.0.1 8888
并发送以下数据:
eddy-2:~ eddy$ telnet 127.0.0.1 8888
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
test
test
回到 iOS 模拟器,它运行但没有发送任何内容,我不确定我是否正在执行初始化套接字的正确方法(我对 PhoneGap 都是新手)和 Xcode)。我按照自述文件中的步骤进行操作,但它没有提供太多上下文。
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:
- Both asyncsocket.m and asynsocket.h from cocoaasyncsocket
- Both GapSocketCommand.m and GapSocketCommand.h from GapSocket
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 cordova.plist 插件中包含一个带有 GapSocketCommand 字符串和值的条目
you need to include an entry in cordova.plist plugin with GapSocketCommand string and value
有两处错误:
ifconfig
或类似工具找到您的 IP 地址)。例如,它应该如下所示:
另外,不要忘记在
PhoneGap.plist
中注册插件。Two things are wrong:
ifconfig
or similar).As an example, it should look something like this:
Also, don't forget to register the plugin in
PhoneGap.plist
.