Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:34:09+00:00 2026-06-07T09:34:09+00:00

EDIT: I want to implement a quiz-application on Android and Browser via Web Interface.

  • 0

EDIT:
I want to implement a quiz-application on Android and Browser via Web Interface.
I’m looking for a way to communicate between the server and the clients. I tried socket.io but couldn’t get it working with android.

I’m using a node.js server hosted on nodester (nodester.com).
I tried some libs but couldn’t get it working.

I’m now working with einaros/ws from https://github.com/einaros/ws

The server code is:

var clients = [],
numClients = 0;


var WebSocketServer = require('ws').Server,
  wss = new WebSocketServer({port: 20083});

wss.on('connection', function(ws) {

  ws.on('message', function(message) {
    console.log(wss.clients);
    console.log('received: %s', message);
    incomingMessage(message, ws)
  });

  /*
  ws.on('eigenesEvent', function(message) {
    console.log('eigenes Event ausgelöst: ' + message);
  });
  */
});

function incomingMessage(msg, ws) {
  //console.log(wss.clients);
  var obj = JSON.parse(msg);

    if(obj.type == "connect") {

      for(var i=0;i<clients.length;i++) {
        if(clients[i] == obj.id) {
          ws.send(JSON.stringify({
            to: obj.id,
            message: "name vergeben"
          }));
          return;
        }
      }
      clients[numClients] = obj.id;
      numClients++;
      for(var i=0;i<clients.length;i++) {
        console.log("Client" + i + ": " + clients[i]);
      }
      ws.send(JSON.stringify({
          to: "all",
          message: obj.id + " connected"
      }));
    }

  if(obj.type == "disconnect") {

    for(var i=0;i<clients.length;i++) {
      if(clients[i] == obj.id) {
        clients.splice(i, 1);
        numClients--;
        for(var i=0;i<clients.length;i++) {
          console.log("Client" + i + ": " + clients[i]);
        }
      }
    }

    ws.send(JSON.stringify({
      to: "all",
      message: obj.id + " disconnected"
    }));
    return;
  }

  if(obj.type == "answer") {
    if("id" in obj) {
      if(obj.answer == "a") {
        ws.send(JSON.stringify({
          to: obj.id,
          message: "a is correct"
        }));
      } else {
        ws.send(JSON.stringify({
          to: obj.id,
          message: "answer is incorrect"
        }));
      }
    }
  }

  if(obj.type == "something") {
    if("id" in obj) {
      ws.send(JSON.stringify({
        to: obj.id,
        message: "received: " + obj.message
      }));
    }
  }
}

From a HTML-Site i can connect to the server via:

connect = function() {
    var host = "ws://einaros.nodester.com";

        try{
            socket = new WebSocket(host);
            console.log('WebSocket - status ' + socket.readyState);

            socket.onopen = function(msg) {
                console.log("Welcome - status " + this.readyState);
                socket.send(JSON.stringify({
                    id: model.getClientName(),
                    type: "connect"
                }));
                model.setConnectionStatus(true);
            };

            socket.onmessage = function(msg) {
                console.log("onmessage - msg: " + msg.data);
                checkMessage(msg.data);
            };

            socket.onclose = function(msg) {
                console.log("Disconnected - status " + this.readyState);
                model.setConnectionStatus(false);
            };

        }
        catch(ex){
            console.log(ex);
        }
},

On the Android-Client side i’m using AutobahnAndroid from: http://autobahn.ws/android
The client code for android is:

package ps.mediengestaltung;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;


public class MainActivity extends Activity {



public final WebSocketConnection mConnection = new WebSocketConnection();

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    final String wsuri = "ws://einaros.nodester.com";

    try {
        mConnection.connect(wsuri, new WebSocketHandler() {

            @Override
            public void onOpen() {
                Log.d("TAG", "Status: Connected to " + wsuri);
                mConnection.sendTextMessage("Hello Server!");
            }

            @Override
            public void onTextMessage(String payload) {
                Log.d("TAG", "Got echo: " + payload);
            }

            @Override
            public void onClose(int code, String reason) {
                Log.d("TAG", "Connection lost.");
            }
        });
    } catch (WebSocketException e) {
        Log.d("TAG", e.toString());
    }

}

}

In LogCat i get:
08-01 08:48:13.017: D/TAG(704): Status: Connected to ws://einaros.nodester.com
08-01 08:48:13.167: D/TAG(704): Connection lost.

What am i doing wrong? Any hints?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:34:11+00:00Added an answer on June 7, 2026 at 9:34 am

    The reason could be: Weberknecht only implements the (outdated) Hixie-76 version of WebSocket.

    You might try AutobahnAndroid, which implements the final RFC6455 version of WebSocket.

    Another things: the WebSocket server you are using is no longer maintained (as far as I know). It also only implements Hixie-76 – which is no longer supported by Chrome/Firefox.

    Try one of these:

    • https://github.com/einaros/ws
    • https://github.com/Worlize/WebSocket-Node

    Disclaimer: I am the author of Autobahn and work for Tavendo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a search in my Android application. In this page, first
Because I didn't want to implement a communication protocol for my client-server based application,
I want to implement test feedback in my web application in the following manner
i starter in jqgrid, i want implement inline edit in jqgrid i have this
I want to implement search bar in my application which will filter list view
I want to implement Ajax on my JSF web project. I googled and found
I want to implement the rotation in my application. So I made my canvas
if i want to implement am http server. i create new actor per request.
Possible Duplicate: Uploading and downloading via ftp with iPhone SDK I want to implement
I'm trying to write a proxy server and right now I want to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.