Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  90] [ 2]  / answers: 1 / hits: 108854  / 12 Years ago, thu, august 16, 2012, 12:00:00

I am writing an application for Android 2.3.5 (that will also be compatible with iOS). I wish to transfer data from the app's HTML/Javascript to a Python program on a server (which uses the Twisted engine to retrieve the data).



I've tried many things and looked at various forums, answers, tutorials, and web pages--including most of them here--and can't find an answer. Here's the relevant Javascript I have in my index.html file:



<script src=socket-lib/socket.io.js></script>
<script type=text/javascript charset=utf-8>
function sendData() {
try {
var socket = io.connect('http://mywebsite.com:12345');
socket.on('connect', function(data) {
socket.send('Hello.');
socket.on('message', function (msg) {
socket.send('This is where I send data?');
});
});
}
catch(err) {
alert('ERROR: socket.io encountered a problem:nn' + err);
}
} // end of sendData


If you can't tell, I'm still pretty confused how this works; I can't even test anything. The error that keeps coming up is ReferenceError: io is not defined. Some sites used something like var io = require('socket.io');. But then it results in the same error: ReferenceError: require is not defined.



I put the socket-lib folder in assets/www, where any other Javascript source should go. This is also where the index.html file is. Many sites use <script src=/socket.io/socket.io.js></script>, but this makes no sense to me. Many sites also imply the use of node.js, but I never see it anywhere.



How can I make this work?



Reply edits:



I tried it in Chrome, and it's giving me an Uncaught ReferenceError: require is not defined for the socket.io.js file. So I decide to source in require.js right before it. Then it gives the error Uncaught Error: Module name socket.io-client has not been loaded yet for context. Since I'm not using this, I care not. When I try the connection, though, it gives the same io is not defined error. When I define it as var io = require('socket.io'), the error is Error: Module name socket.io has not been loaded yet for context: _ http://requirejs.org/docs/errors.html#notloaded. I looked at the website, and it doesn't help me at all. When I try to put require as a function argument, another error occurs: TypeError: undefined is not a function.


More From » android

 Answers
22

I found the answer for anyone who gets immensely confused by the horrible lack of documentation of socket.io.



You cannot source /socket-lib/socket.io.js,



you must source http://yourwebsite.com:12345/socket.io/socket.io.js.



The server automatically does the rest for you.


[#83590] Wednesday, August 15, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chase

Total Points: 78
Total Questions: 106
Total Answers: 93

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
chase questions
Thu, Mar 31, 22, 00:00, 2 Years ago
Thu, Jul 1, 21, 00:00, 3 Years ago
Sat, Dec 12, 20, 00:00, 4 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
;