Browse Source

remove namespaces

main
Hendrik Langer 4 years ago
parent
commit
84fbf42f22
  1. 8
      raspberry/roberto/camera/camera_gstreamer_webrtc.py
  2. 12
      raspberry/roberto/views/frontend/templates/gamepad.js
  3. 12
      raspberry/roberto/views/frontend/templates/index.html
  4. 38
      raspberry/roberto/views/websocket/routes.py

8
raspberry/roberto/camera/camera_gstreamer_webrtc.py

@ -162,7 +162,7 @@ class WebRTCCamera(Thread):
def connect(self): def connect(self):
while not sio.sid: while not sio.sid:
print("connecting camera websocket..") print("connecting camera websocket..")
sio.connect(self.server, namespaces=['/webrtc'], transports=['websocket']) sio.connect(self.server, transports=['websocket'])
time.sleep(1) time.sleep(1)
print('camera sid is', sio.sid) print('camera sid is', sio.sid)
self.sid = sio.sid self.sid = sio.sid
@ -343,7 +343,7 @@ class WebRTCCamera(Thread):
def on_ice_candidate(self, _, mlineindex, candidate, client_sid): def on_ice_candidate(self, _, mlineindex, candidate, client_sid):
print("on_ice_candidate [%s]" % client_sid) print("on_ice_candidate [%s]" % client_sid)
icemsg = json.dumps({'type': 'ice', 'data': {'candidate': candidate, 'sdpMLineIndex': mlineindex}, 'sid': client_sid}) icemsg = json.dumps({'type': 'ice', 'data': {'candidate': candidate, 'sdpMLineIndex': mlineindex}, 'sid': client_sid})
sio.emit('message', data=icemsg, namespace='/webrtc') sio.emit('message', data=icemsg)
# Offer created by our pipeline, to be sent to the peer # Offer created by our pipeline, to be sent to the peer
def on_offer_created(self, promise, _, client_sid, __): def on_offer_created(self, promise, _, client_sid, __):
@ -357,10 +357,10 @@ class WebRTCCamera(Thread):
text = offer.sdp.as_text() text = offer.sdp.as_text()
print ('Sending offer:\n%s' % text) print ('Sending offer:\n%s' % text)
msg = json.dumps({'type': 'sdp', 'data': {'type': 'offer', 'sdp': text}, 'sid': client_sid}) msg = json.dumps({'type': 'sdp', 'data': {'type': 'offer', 'sdp': text}, 'sid': client_sid})
sio.emit('message', data=msg, namespace='/webrtc') sio.emit('message', data=msg)
print("test") print("test")
@sio.on('message', namespace='/webrtc') @sio.on('message')
def webrtc_message(data): def webrtc_message(data):
from roberto import webrtccamera from roberto import webrtccamera
# print('WebRTCCamera got Message: {}'.format(data)) # print('WebRTCCamera got Message: {}'.format(data))

12
raspberry/roberto/views/frontend/templates/gamepad.js

@ -7,11 +7,11 @@
* You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. * You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/ */
namespace = '/gamepad'; //namespace = '/gamepad';
var gp_socket = io(namespace, { transports: [ 'websocket' ] }); //var gp_socket = io(namespace, { transports: [ 'websocket' ] });
gp_socket.on('connect', function() { socket.on('connect', function() {
gp_socket.emit('my_event', {data: 'I\'m connected!'}); socket.emit('my_event', {data: 'I\'m connected!'});
}); });
var haveEvents = 'GamepadEvent' in window; var haveEvents = 'GamepadEvent' in window;
@ -69,7 +69,7 @@ function updateStatus() {
} }
if (axesChanged) { if (axesChanged) {
gp_socket.emit('axes', {0: controller.axes[0], 1: controller.axes[1], 3: controller.axes[3], 4: controller.axes[4]}); socket.emit('axes', {0: controller.axes[0], 1: controller.axes[1], 3: controller.axes[3], 4: controller.axes[4]});
} }
} }
rAF(updateStatus); rAF(updateStatus);
@ -144,7 +144,7 @@ var joystick_axes = {
setInterval(function(){ setInterval(function(){
if (joystick_axes.moved) { if (joystick_axes.moved) {
gp_socket.emit('axes', {0: joystick_axes.x1, 1: joystick_axes.y1, 3: joystick_axes.x2, 4: joystick_axes.y2}); socket.emit('axes', {0: joystick_axes.x1, 1: joystick_axes.y1, 3: joystick_axes.x2, 4: joystick_axes.y2});
joystick_axes.moved = false; joystick_axes.moved = false;
} }
}, 50); }, 50);

12
raspberry/roberto/views/frontend/templates/index.html

@ -87,7 +87,7 @@
<script type="text/javascript"> <script type="text/javascript">
'use strict'; 'use strict';
var html5VideoElement; var html5VideoElement;
var wsUrl = "wss://" + window.location.hostname + ":" + window.location.port + "/webrtc"; var wsUrl = "wss://" + window.location.hostname + ":" + window.location.port;
var socket = io.connect(wsUrl, { autoConnect: false, transports: [ 'websocket' ] }); var socket = io.connect(wsUrl, { autoConnect: false, transports: [ 'websocket' ] });
var webrtcPeerConnection; var webrtcPeerConnection;
var webrtcConfiguration; var webrtcConfiguration;
@ -225,16 +225,6 @@ function getConnectionStats() {
var statsInterval = window.setInterval(getConnectionStats, 1000); var statsInterval = window.setInterval(getConnectionStats, 1000);
setInterval(function(){
if (socket.connected) {
socket.emit('ping_message', { "type": "ping" });
}
}, 10000);
socket.on('ping_message', (data) => {
console.log('ping received: ',data);
});
</script> </script>
<script src="{{ url_for('frontend.gamepad_js') }}"></script> <script src="{{ url_for('frontend.gamepad_js') }}"></script>

38
raspberry/roberto/views/websocket/routes.py

@ -10,23 +10,7 @@ from roberto import socketio
#### routes #### #### routes ####
################ ################
@socketio.on('my_event', namespace='/test') @socketio.on('axes')
def test_message(message):
emit('my response', {'data': message['data']})
@socketio.on('my broadcast event', namespace='/test')
def test_message(message):
emit('my response', {'data': message['data']}, broadcast=True)
@socketio.on('connect', namespace='/test')
def test_connect():
emit('my response', {'data': 'Connected'})
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected')
@socketio.on('axes', namespace='/gamepad')
def gamepad_axes(axes_data): def gamepad_axes(axes_data):
print('GAMEPAD axes') print('GAMEPAD axes')
print(axes_data) print(axes_data)
@ -53,7 +37,7 @@ def applyDeadZone(value, threshold):
new_value = new_value * -1 new_value = new_value * -1
return new_value return new_value
@socketio.on('text', namespace='/gamepad') @socketio.on('text')
def display_text(text): def display_text(text):
print("display_text()") print("display_text()")
print(text['data']) print(text['data'])
@ -73,7 +57,7 @@ ROOM = 'default'
def camera(): def camera():
return render_template('camera.html', room=ROOM) return render_template('camera.html', room=ROOM)
@socketio.on('message', namespace='/webrtc') # ok @socketio.on('message') # ok
def webrtc_message(data): def webrtc_message(data):
sid = request.sid sid = request.sid
print('Message from {}: {}'.format(sid, data)) print('Message from {}: {}'.format(sid, data))
@ -88,15 +72,9 @@ def webrtc_message(data):
data['from_sid'] = sid data['from_sid'] = sid
else: else:
print("unknown message type") print("unknown message type")
socketio.emit('message', data=data, room=ROOM, namespace='/webrtc', skip_sid=sid) socketio.emit('message', data=data, room=ROOM, skip_sid=sid)
@socketio.on('ping_message', namespace='/webrtc')
def ping_message(data):
sid = request.sid
print("Received ping from %s" % sid)
socketio.emit('ping_message', data={ 'type':'pong' }, room=sid, namespace='/webrtc')
@socketio.on('disconnect', namespace='/webrtc') @socketio.on('disconnect')
def disconnect(): def disconnect():
sid = request.sid sid = request.sid
print("Received Disconnect message from %s" % sid) print("Received Disconnect message from %s" % sid)
@ -106,7 +84,7 @@ def disconnect():
if sid != webrtccamera.sid: if sid != webrtccamera.sid:
serial.del_callback('B', display_battery) serial.del_callback('B', display_battery)
@socketio.on('connect', namespace='/webrtc') @socketio.on('connect')
def connect(): def connect():
sid = request.sid sid = request.sid
print("Received Connect message from %s" % sid) print("Received Connect message from %s" % sid)
@ -126,5 +104,5 @@ def default_error_handler(e):
print('%s: %s' % (key, repr(request[key]))) print('%s: %s' % (key, repr(request[key])))
def display_battery(val): def display_battery(val):
socketio.emit('battery', data=val, room=ROOM, namespace='/webrtc') socketio.emit('battery', data=val, room=ROOM)
socketio.emit('battery', data=val, namespace='/webrtc') #socketio.emit('battery', data=val)

Loading…
Cancel
Save