I created the following ‘hello world’ HTML file using Phonegap:
<!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>
When I ran this locally (Chrome on Windows 7) I received the following output:
Exception Caught: InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state
Which is perfect because I know that I am forcing a ‘publish’ before a connection has been established, and I do this so I can receive an error.
When I ran as a Phonegap Android Application (apk) deployed to my Samsung Galaxy S3, I received the following output:
Exception Caught: Error: INVALID_STATE_ERR: DOM Exception 11
When I ran this on the emulator I received the error:
Exception Caught: InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.
I then removed the line “ws.publish("myTopic",{myMessage: 'hello world!'});” and ran it again on the emulator and received the following output:
Device Ready!
WebSockets Ready!
which is exactly what I want to see. I then deployed to my android phone and the only output I get is:
Device Ready!
Has anyone run an app with XSockets.Net as a Phonegap package? Are there any special tricks/configurations to get this working? I know that in Phonegap config.xml you must add URLs to be access by the app, so I added the following entries to 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"/>
I still only receive “Device Ready!” when running the app on my phone. I currently only have this working on the emulator.
EDIT: I removed all access tags in my-app\www\config.xml
and only used:
<access origin="*" />
however my app still only shows Device Ready!
. If everything works, then I expect Websockets Ready!
in the output as well.
Any help is greatly appreciated!!