我在客户端有以下代码:
player_id = Math.round(Math.random() * 5000);
your_sock = io.connect('http://' + document.domain + ':' + location.port + '/'); //io.connect(''); //io.connect('http://' + document.domain + ':' + location.port);
your_sock.on('connect', function(){
your_sock.emit('player_connect', {'id': player_id});
console.log('I have connected with ID ' + player_id);
});
以及服务器端的此代码:
@socker.on('connect')
def handle_connect():
print('THE HANDLE CONNECT FUNCTION WAS WRITTEN')
try:
socker.emit('testing', {'hello': 'hi'})
except Exception as e:
print(e)
我在Chrome开发者日志中看到“我已与ID 27连接”,这意味着客户端套接字连接功能已运行,但是服务器端未记录“ HANDLE CONNECT FUNCTION WAS RUN”。 请注意,我在套接字函数之外添加的任何其他日志都会记录下来,因此打印到heroku日志不是问题
Requirements.txt
click==6.7
dnspython==1.15.0
eventlet==0.24.1
Flask==1.0.2
flask-heroku==0.1.9
Flask-SocketIO==3.0.1
greenlet==0.4.14
gunicorn==19.9.0
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
monotonic==1.5
python-engineio==2.2.0
python-socketio==2.0.0
six==1.11.0
Werkzeug==0.14.1
Procfile:
web: gunicorn --worker-class eventlet -w 1 --no-sendfile --log-level=DEBUG --log-file=- --preload --chdir nickdima wsgi:app
我的procfile中引用的wsgi.py文件:
import sys
sys.path.insert(0, "/app/nickdima")
from __init__ import create_app
app = create_app()
Heroku日志:
2018-09-05T22:11:03.936310+00:00 app[web.1]: hello logs
2018-09-05T22:11:03.938061+00:00 app[web.1]: 10.31.74.203 - - [05/Sep/2018:22:11:03 +0000] "GET /test_pong/test_pong_game HTTP/1.1" 200 1022 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
2018-09-05T22:11:03.938237+00:00 app[web.1]: [2018-09-05 22:11:03 +0000] [9] [DEBUG] Closing connection.
2018-09-05T22:11:03.982684+00:00 app[web.1]: [2018-09-05 22:11:03 +0000] [9] [DEBUG] Closing connection.
2018-09-05T22:11:04.308695+00:00 app[web.1]: [2018-09-05 22:11:04 +0000] [9] [DEBUG] GET /socket.io/
2018-09-05T22:11:04.313499+00:00 app[web.1]: 10.31.74.203 - - [05/Sep/2018:22:11:04 +0000] "GET /socket.io/?EIO=3&transport=polling&t=1536185465659-0 HTTP/1.1" 200 119 "http://www.nickdima.com/test_pong/test_pong_game" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
2018-09-05T22:11:04.313924+00:00 app[web.1]: [2018-09-05 22:11:04 +0000] [9] [DEBUG] Closing connection.
2018-09-05T22:11:04.352762+00:00 app[web.1]: [2018-09-05 22:11:04 +0000] [9] [DEBUG] POST /socket.io/
2018-09-05T22:11:04.359377+00:00 app[web.1]: 10.31.74.203 - - [05/Sep/2018:22:11:04 +0000] "POST /socket.io/?EIO=3&transport=polling&t=1536185465714-1&sid=bcd9d3c54362405a91a6ec22c4633c1c HTTP/1.1" 200 2 "http://www.nickdima.com/test_pong/test_pong_game" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
2018-09-05T22:11:04.360509+00:00 app[web.1]: [2018-09-05 22:11:04 +0000] [9] [DEBUG] Closing connection.
2018-09-05T22:11:04.365782+00:00 app[web.1]: [2018-09-05 22:11:04 +0000] [9] [DEBUG] GET /socket.io/
2018-09-05T22:11:04.400864+00:00 app[web.1]: [2018-09-05 22:11:04 +0000] [9] [DEBUG] GET /socket.io/
2018-09-05T22:11:04.422792+00:00 app[web.1]: 10.9.255.135 - - [05/Sep/2018:22:11:04 +0000] "GET /socket.io/?EIO=3&transport=polling&t=1536185465722-2&sid=bcd9d3c54362405a91a6ec22c4633c1c HTTP/1.1" 200 4 "http://www.nickdima.com/test_pong/test_pong_game" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
2018-09-05T22:11:04.423589+00:00 app[web.1]: [2018-09-05 22:11:04 +0000] [9] [DEBUG] Closing connection.
2018-09-05T22:11:04.312971+00:00 heroku[router]: at=info method=GET path= "/socket.io/?EIO= 3&transport= polling&t=1536185465659-0" host=www.nickdima.com request_id=89f6de3d-cfa2-41eb-879e-7aaba5052a60 fwd="71.206.187.35" dyno=web.1 connect=0ms service=18ms status=200 bytes=419 protocol=http
2018-09-05T22:11:04.357827+00:00 heroku[router]: at=info method=POST path= "/socket.io/?EIO= 3&transport= polling&t= 1536185465714-1&sid=bcd9d3c54362405a91a6ec22c4633c1c" host=www.nickdima.com request_id=d9505e27-b0c9-49b0-9a2e-0ff31a832e7f fwd="71.206.187.35" dyno=web.1 connect=0ms service=11ms status=200 bytes=260 protocol=http
2018-09-05T22:11:04.422606+00:00 heroku[router]: at=info method=GET path= "/socket.io/?EIO= 3&transport= polling&t= 1536185465722-2&sid=bcd9d3c54362405a91a6ec22c4633c1c" host=www.nickdima.com request_id=d0c37303-d208-4a27-967b-07c03162be1d fwd="71.206.187.35" dyno=web.1 connect=1ms service=65ms status=200 bytes=254 protocol=http
2018-09-05T22:11:03.937943+00:00 heroku[router]: at=info method=GET path="/test_pong/test_pong_game" host=www.nickdima.com request_id=e489d0f1-4a9a-4854-8e11-a43134dae135 fwd="71.206.187.35" dyno=web.1 connect=0ms service=9ms status=200 bytes=1184 protocol=http
2018-09-05T22:12:25.105722+00:00 app[web.1]: [2018-09-05 22:12:25 +0000] [9] [DEBUG] Closing connection.
2018-09-05T22:14:41.196614+00:00 app[web.1]: [2018-09-05 22:14:41 +0000] [9] [DEBUG] Closing connection.
请注意“ Hello Logs”,这是一条单独的python语句,该语句会在页面加载时打印,以确保我可以打印到heroku日志
预加载日志:
chdir: /app/nickdima
2018-09-05T22:38:29.417586+00:00 app[web.1]: daemon: False
2018-09-05T22:38:29.417589+00:00 app[web.1]: pidfile: None
2018-09-05T22:38:29.417592+00:00 app[web.1]: user: 5991
2018-09-05T22:38:29.417593+00:00 app[web.1]: group: 5991
2018-09-05T22:38:29.417588+00:00 app[web.1]: raw_env: []
2018-09-05T22:38:29.417595+00:00 app[web.1]: umask: 0
2018-09-05T22:38:29.417591+00:00 app[web.1]: worker_tmp_dir: None
2018-09-05T22:38:29.417596+00:00 app[web.1]: initgroups: False
2018-09-05T22:38:29.417598+00:00 app[web.1]: tmp_upload_dir: None
2018-09-05T22:38:29.417606+00:00 app[web.1]: secure_scheme_headers: {'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-SSL': 'on'}
2018-09-05T22:38:29.417608+00:00 app[web.1]: forwarded_allow_ips: ['*']
2018-09-05T22:38:29.417609+00:00 app[web.1]: accesslog: -
2018-09-05T22:38:29.417611+00:00 app[web.1]: disable_redirect_access_to_syslog: False
2018-09-05T22:38:29.417616+00:00 app[web.1]: access_log_format: %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"
2018-09-05T22:38:29.417618+00:00 app[web.1]: errorlog: -
2018-09-05T22:38:29.417619+00:00 app[web.1]: loglevel: DEBUG
2018-09-05T22:38:29.417620+00:00 app[web.1]: capture_output: False
2018-09-05T22:38:29.417622+00:00 app[web.1]: logger_class: gunicorn.glogging.Logger
2018-09-05T22:38:29.417623+00:00 app[web.1]: logconfig: None
2018-09-05T22:38:29.417625+00:00 app[web.1]: logconfig_dict: {}
2018-09-05T22:38:29.417626+00:00 app[web.1]: syslog_addr: udp://localhost:514
2018-09-05T22:38:29.417627+00:00 app[web.1]: syslog: False
2018-09-05T22:38:29.417629+00:00 app[web.1]: syslog_prefix: None
2018-09-05T22:38:29.417630+00:00 app[web.1]: syslog_facility: user
2018-09-05T22:38:29.417632+00:00 app[web.1]: enable_stdio_inheritance: False
2018-09-05T22:38:29.417634+00:00 app[web.1]: statsd_prefix:
2018-09-05T22:38:29.417636+00:00 app[web.1]: proc_name: None
2018-09-05T22:38:29.417633+00:00 app[web.1]: statsd_host: None
2018-09-05T22:38:29.417639+00:00 app[web.1]: pythonpath: None
2018-09-05T22:38:29.417646+00:00 app[web.1]: on_starting: <function OnStarting.on_starting at 0x7f9b84c2d1e0>
2018-09-05T22:38:29.417637+00:00 app[web.1]: default_proc_name: wsgi:app
2018-09-05T22:38:29.417651+00:00 app[web.1]: pre_fork: <function Prefork.pre_fork at 0x7f9b84c2d510>
2018-09-05T22:38:29.417640+00:00 app[web.1]: paste: None
2018-09-05T22:38:29.417652+00:00 app[web.1]: post_fork: <function Postfork.post_fork at 0x7f9b84c2d620>
2018-09-05T22:38:29.417648+00:00 app[web.1]: on_reload: <function OnReload.on_reload at 0x7f9b84c2d2f0>
2018-09-05T22:38:29.417656+00:00 app[web.1]: worker_int: <function WorkerInt.worker_int at 0x7f9b84c2d840>
2018-09-05T22:38:29.417649+00:00 app[web.1]: when_ready: <function WhenReady.when_ready at 0x7f9b84c2d400>
2018-09-05T22:38:29.417657+00:00 app[web.1]: worker_abort: <function WorkerAbort.worker_abort at 0x7f9b84c2d950>
2018-09-05T22:38:29.417658+00:00 app[web.1]: pre_exec: <function PreExec.pre_exec at 0x7f9b84c2da60>
2018-09-05T22:38:29.417654+00:00 app[web.1]: post_worker_init: <function PostWorkerInit.post_worker_init at 0x7f9b84c2d730>
2018-09-05T22:38:29.417660+00:00 app[web.1]: pre_request: <function PreRequest.pre_request at 0x7f9b84c2db70>
2018-09-05T22:38:29.417666+00:00 app[web.1]: nworkers_changed: <function NumWorkersChanged.nworkers_changed at 0x7f9b84c2df28>
2018-09-05T22:38:29.417664+00:00 app[web.1]: worker_exit: <function WorkerExit.worker_exit at 0x7f9b84c2de18>
2018-09-05T22:38:29.417669+00:00 app[web.1]: proxy_protocol: False
2018-09-05T22:38:29.417670+00:00 app[web.1]: proxy_allow_ips: ['127.0.0.1']
2018-09-05T22:38:29.417661+00:00 app[web.1]: post_request: <function PostRequest.post_request at 0x7f9b84c2dbf8>
2018-09-05T22:38:29.417663+00:00 app[web.1]: child_exit: <function ChildExit.child_exit at 0x7f9b84c2dd08>
2018-09-05T22:38:29.417673+00:00 app[web.1]: certfile: None
2018-09-05T22:38:29.417667+00:00 app[web.1]: on_exit: <function OnExit.on_exit at 0x7f9b84cc00d0>
2018-09-05T22:38:29.417675+00:00 app[web.1]: ssl_version: 2
2018-09-05T22:38:29.417677+00:00 app[web.1]: ca_certs: None
2018-09-05T22:38:29.417680+00:00 app[web.1]: do_handshake_on_connect: False
2018-09-05T22:38:29.417672+00:00 app[web.1]: keyfile: None
2018-09-05T22:38:29.417760+00:00 app[web.1]: raw_paste_global_conf: []
2018-09-05T22:38:29.417676+00:00 app[web.1]: cert_reqs: 0
2018-09-05T22:38:29.417679+00:00 app[web.1]: suppress_ragged_eofs: True
2018-09-05T22:38:29.417682+00:00 app[web.1]: ciphers: TLSv1
2018-09-05T22:38:30.303852+00:00 app[web.1]: [2018-09-05 22:38:30 +0000] [4] [INFO] Starting gunicorn 19.9.0
2018-09-05T22:38:30.304961+00:00 app[web.1]: [2018-09-05 22:38:30 +0000] [4] [DEBUG] Arbiter booted
2018-09-05T22:38:30.305184+00:00 app[web.1]: [2018-09-05 22:38:30 +0000] [4] [INFO] Listening at: http://0.0.0.0:46524 (4)
2018-09-05T22:38:30.305414+00:00 app[web.1]: [2018-09-05 22:38:30 +0000] [4] [INFO] Using worker: eventlet
2018-09-05T22:38:30.313804+00:00 app[web.1]: [2018-09-05 22:38:30 +0000] [9] [INFO] Booting worker with pid: 9
2018-09-05T22:38:30.361254+00:00 app[web.1]: [2018-09-05 22:38:30 +0000] [4] [DEBUG] 1 workers
2018-09-05T22:38:30.851461+00:00 heroku[web.1]: State changed from starting to up
2018-09-05T22:38:30.832213+00:00 app[web.1]: [2018-09-05 22:38:30 +0000] [9] [DEBUG] Closing connection.
2018-09-05T22:38:30.833331+00:00 app[web.1]: [2018-09-05 22:38:30 +0000] [9] [DEBUG] Closing connection.
答案 0 :(得分:0)
这确实是一个导入错误,其中事件处理程序未导入包含我的应用程序工厂的__init__.py文件中,因此未注册到我的应用程序。
要解决此问题,我在包含以下内容的根目录中添加了一个socket_instance.py文件
from flask_socketio import SocketIO
并将其导入到我的__init__.py文件以及所有事件处理程序创建文件中。
然后从任何文件中,我现在都可以这样导入我的socketio对象:
from socket_instance import socketio