Wednesday, May 6, 2015

Working more with NodeJS/SocketIO (5/5)

Today I started on one of my other projects and integrating Socket.io into it. I now have a go-to Socket.io script, as seen below.

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:

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');
});
view raw gistfile1.js hosted with ❤ by GitHub

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:

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();
view raw gistfile1.java hosted with ❤ by GitHub

1 comment:

  1. 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.

    I'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.

    ReplyDelete