As you can see, using the NodeJS modules http, express, and socket.io, a server is created. I then went and implemented this into my Android app like so:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require('express')(); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
app.get('/', function (req, res) { | |
res.sendfile('index.html'); | |
}); | |
io.on('connection', function (socket) { | |
console.log('a user connected'); | |
socket.on('disconnect', function () { | |
console.log('user disconnected'); | |
}); | |
socket.on('testEvent', function(data){ | |
// Do stuff with TestEventData here | |
}); | |
}); | |
}); | |
http.listen(3000, function () { | |
console.log("================================="); | |
console.log("Now running Server v0.2.0"); | |
console.log("================================="); | |
console.log('[Server] Listening on *:3000'); | |
}); |
And just like that (granted the Android device has an internet connection), the socket works! You can now use this as a server to send and receive data to one or multiple devices. I decided to change the event for each device, so the event is sent like 'socket.emit('deviceEvent ' + deviceId, informationHere)'. That way, the server can talk to individual Android devices. The Android code is as follows:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void startConnection(final String roomId) { | |
try { | |
mSocket = IO.socket("yourSocketIP"); // TODO Set our website in here with URL | |
mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
// Send some data | |
} | |
}).on("yourData", new Emitter.Listener() { | |
// Android device is now listening so when yourData is called, it does the following... | |
@Override | |
public void call(Object... args) { | |
JSONObject jsonObject = new JSONObject(); | |
try { | |
jsonObject = new JSONObject(args[0].toString()); | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
// Code is called when the device disconnects from the server | |
} | |
}); | |
mSocket.connect(); | |
} catch (URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void sendDataToServer(final String roomId) { | |
new CountDownTimer(11000, 1000) { | |
@Override | |
public void onTick(long millisUntilFinished) { | |
// This method is called every 1 second. I'm using it to send more data from the server. | |
} | |
@Override | |
public void onFinish() { | |
sendDataToServer(roomId); | |
} | |
}.start(); |
Cool! I'm glad to see you doing more Android work, actually. Since with one month to go, I need to give you a grade for Computer Science II, and aside from the UMD programming contest you have only been very tangentially relating the CSII curriculum this quarter.
ReplyDeleteI'm thinking I would like you to take the last eIMACs test (number 26 on Android development) as your final exam. Would that work? Let's talk in class today.