我开始使用 Socketio,但遇到了问题。我无法向我的烧瓶会话发送简单的消息。我的Java代码:
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();
}
}
}
我的 Python 代码:
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)
错误:
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
我希望有人能够帮助我解决这个问题。我到处搜索,我找不到我的答案。