0

I'm trying to write a JavaScript server running on node.js using Socket.io that communicates with the client which is my android app (java class). Since I need a combination of a JS server and java client that utilizes the Socket.io (or any other efficient websockets) framework, I found the Gottox/socket-io.-java-client implementation that seemed like it might do the job.

Problem: I'm very new to socket programming and working with Github projects. I tried following the simplistic approach mentioned on the project but ran into build errors with Eclipse, specifically relating to Archiving issues with WebSocket.jar and json-org.jar.

Unable to solve this, I tried importing the project into Android Studio when I ran into a whole different bug, which I'm very unfamiliar with.

I just want to make sure I'm working with this project right in the first place. This is what my client class looks like:

package com.example.culami;

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;

import org.json.JSONException;
import org.json.JSONObject;

public class AcknowledgeExample implements IOCallback {
    private SocketIO socket;

    /**
     * @param args
     */
    /*public static void main(String[] args) {
        try {
            new AcknowledgeExample();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    public AcknowledgeExample() throws Exception 
    { 
        socket = new SocketIO();
        socket.connect("http://192.168.0.108:3000/", this);

        // Sends a string to the server.
        socket.send("Hello Server");

        // Sends a JSON object to the server.
        socket.send(new JSONObject().put("key", "value").put("key2",
                "another value"));

        // Emits an event to the server.
        socket.emit("event", "argument1", "argument2", 13.37);
    }

    @Override
    public void onMessage(JSONObject json, IOAcknowledge ack) {
        try {
            System.out.println("Server said:" + json.toString(2));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMessage(String data, IOAcknowledge ack) {
        System.out.println("Server said: " + data);
    }

    @Override
    public void onError(SocketIOException socketIOException) {
        System.out.println("an Error occured");
        socketIOException.printStackTrace();
    }

    @Override
    public void onDisconnect() {
        System.out.println("Connection terminated.");
    }

    @Override
    public void onConnect() {
        System.out.println("Connection established");
    }

    @Override
    public void on(String event, IOAcknowledge ack, Object... args) {
        System.out.println("Server triggered event '" + event + "'");
    }

}

I imported the socketio.jar and even WebSocket.jar and json-org.jar because it seemed these were needed as well. Any feedback on what I'm doing wrong or how I should incorporate this library in my android project will be highly appreciated since I've already spent countless hours trying to debug the build issue.

Note: I'm using Android L, API 21 and jdk1.7 to run this project.

Community
  • 1
  • 1
Darth Coder
  • 1,738
  • 7
  • 33
  • 56

1 Answers1

1

Instead you can add

socket.io-client-0.1.0.jar

The link for his http://mvnrepository.com/artifact/com.github.nkzawa/socket.io-client/0.1.0