小夥伴給了一個socket.io和cordova 通訊的鏈接;link
按照教程來發現教程不是很全面;
如今從新弄一下步驟:javascript
1. 安裝Socket.iohtml
npm install socket.io
2. 運行node app.js
app.js內容以下:java
var server = require('http').createServer(); var io = require('socket.io')(server); io.sockets.on('connection', function (socket) { console.log('socket connected'); socket.on('disconnect', function () { console.log('socket disconnected'); }); socket.emit('text', 'wow. such event. very real time.'); }); server.listen(3000,'192.168.0.0');//注意到這裏要填寫IP地址
3.安裝cordovanode
npm install -g cordova
cordova create test
cd test
cordova platform add android
5.
在www/index.html文件下添加android
<script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="http://cdn.socket.io/socket.io-1.0.3.js"></script> <script type="text/javascript" src="js/index.js"></script>
6.在index.js 文件下添加一下的代碼:apache
document.addEventListener('deviceready', function() { var socket = io.connect('http://192.168.0.0:3000');//注意到這裏要填寫IP地址 socket.on('connect', function() { socket.on('text', function(text) { alert(text); }); }); });