2

I started to use Socketio, and I got a problem. I can't send a simple message to my flask session. My Java code:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

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

import java.net.URISyntaxException;

import io.socket.client.IO;
import io.socket.client.Socket;

public class MainActivity extends AppCompatActivity {

    Socket socket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            socket = IO.socket("http://192.168.8.101:8080/send");
        } catch (URISyntaxException e) {

        }

    }

    public void TestButton(View v){
        Log.d("send","before send");
        JSONObject obj = new JSONObject();
        try {
            obj.put("message", "hi");
            obj.put("binary", new byte[42]);
            socket.emit("mes", obj);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

My Python code:

from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
socketio = SocketIO(app)


@socketio.on("mes", namespace="/send")
def chat_message(message):
    print("message:")


if __name__ == "__main__":
    print("start")
    socketio.run(app, host="0.0.0.0", port=8080)

The error:

TypeError: a bytes-like object is required, not 'str' <Greenlet at 0x24118773178: _handle_and_close_when_done(<bound method WSGIServer.handle of <WSGIServer at , <bound method StreamServer.do_close of <WSGIServer, (<gevent._socket3.socket [closed]  object, fd=-1, )> failed with TypeError

I hope someone is able to help me with this problem. I've searched everywhere and i can't find my answer.

1
  • When you post a Python error, please include the complete stack trace, as that provides additional information and context for the error. Commented Jul 9, 2016 at 21:47

1 Answer 1

1

I think you use python3 with the gevent and gevent-websocket plugin. gevent-websocket does not support python3 yet.

You have to options here:

  1. Uninstall gevent and gevent-websocket and use eventlet.
  2. Fix the code geventwebsocket/handler.py at line 236.

    if b'101' not in self.status:
    

I hope I could help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.