我正在尝试创建一个简单的应用,该应用使用套接字在Android / IOS应用和服务器之间进行通信。为此,我使用cordova并将其作为Sockets.IO插件。目前,我陷入困境,似乎无法弄清楚自己在做什么,因为在我连接到服务器时
var socket = connection('http://localhost:3000');
继续疯狂地建立联系。
服务器端结果:
server.js
var server = require('http').createServer();
var io = require('socket.io')(server);
var connectCounter=0;
io.sockets.on('connection', function (socket) {
connectCounter++;
console.log('a user connected: ' + socket.id);
console.log('# ' + connectCounter);
socket.emit("test", "ping");
socket.on('disconnect', function () {
console.log('socket disconnected');
connectCounter--;
console.log('# ' + connectCounter);
});
socket.on( 'message', function( data ) {
console.log( 'Message received ' + data.message );
io.sockets.emit( 'message', { message: data.message } );
});
socket.emit('text', 'wow. such event. very real time.');
});
server.listen(3000);
index.js
var socket = io.connect('http://localhost:3000');
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
this.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
index.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.4.3.min.js"></script>
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
<button class="ui-btn ui-corner-all" id="SendButton">Send</button>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/socket.io-1.0.3.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
/*socket.on('connect', function() {
socket.on('text', function(text) {
console.log('Received from socket: ' + text);
});
});
*/
$( "#SendButton" ).click( function() {
var msg = $( "#messageText" ).val();
socket.emit( 'message', 'hallo' );
return false;
});
</script>
</body>
</html>
最后要在服务器端使用ESP8266,但目前我在PC上运行测试并遇到上述问题。我在浏览器,Android(仿真器/电话),IOS(仿真器)和相同的结果上尝试此操作。