我使用 Phonegap 创建了以下“hello world”HTML 文件:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="UTF-8"/>
<title>Hello World</title>
<script src="cordova.js"></script>
<script src="phonegap.js"></script>
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/jquery-migrate-1.2.1.min.js"></script>
<script src="js/XSockets.latest.js"></script>
<script type="text/javascript">
function init()
{
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady()
{
document.write("Device Ready!");
try
{
var ws;
ws = new XSockets.WebSocket('ws://joinaspot.com:4509/Generic');
//ws.publish("myTopic",{myMessage: 'hello world!'});
ws.onopen = function(connection)
{
document.write("<br/>WebSockets Ready!<br/>");
document.write(JSON.stringify(connection));
};
ws.on(XSockets.Events.onError, function (err)
{
document.write("<br/>Error: " + err);
});
}
catch(e)
{
document.write("<br/>Exception Caught: " + e);
}
}
</script>
</head>
<body onLoad="init();">
</body>
</html>
当我在本地运行它(Windows 7 上的 Chrome)时,我收到以下输出:
Exception Caught: InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state
这是完美的,因为我知道我在建立连接之前强制“发布”,我这样做是为了收到错误。
当我作为部署到三星 Galaxy S3 的 Phonegap Android 应用程序 (apk) 运行时,我收到以下输出:
Exception Caught: Error: INVALID_STATE_ERR: DOM Exception 11
当我在模拟器上运行它时,我收到了错误:
Exception Caught: InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.
然后我删除了“ws.publish("myTopic",{myMessage: 'hello world!'});”行 并在模拟器上再次运行它并收到以下输出:
Device Ready!
WebSockets Ready!
这正是我想看到的。然后我部署到我的安卓手机上,我得到的唯一输出是:
Device Ready!
有没有人使用 XSockets.Net 作为 Phonegap 包运行应用程序?有什么特殊的技巧/配置可以让它工作吗?我知道在 Phonegap config.xml 中,您必须添加应用程序可以访问的 URL,因此我在 config.xml 中添加了以下条目:
<access origin="http://127.0.0.1*"/> <!-- allow local pages -->
<access origin="http://joinaspot.com" subdomains="true" />
<access origin="ws://joinaspot.com" subdomains="true" />
<access origin="http://joinaspot.com:4509/Generic"/>
<access origin="ws://joinaspot.com:4509/Generic"/>
我仍然只收到“设备就绪!” 在我的手机上运行应用程序时。我目前只在模拟器上工作。
编辑:我删除了所有访问标签my-app\www\config.xml
,只使用了:
<access origin="*" />
但是我的应用程序仍然只显示Device Ready!
。如果一切正常,那么我也期望Websockets Ready!
输出。
任何帮助是极大的赞赏!!