-
Notifications
You must be signed in to change notification settings - Fork 70
/
websocketserver.py
188 lines (141 loc) · 6.41 KB
/
websocketserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import math
import random
import json
import socket #just for exception handling for restarts here
import circuits
import eventlet
from eventlet import websocket
from PythonQt.QtGui import QVector3D as Vec3
from PythonQt.QtGui import QQuaternion as Quat
import naali
import mathutils
import async_eventlet_wsgiserver
SPAWNPOS = 131.806, 48.9571, 28.7691 #at the end of pier in w.r.o:9000
class NaaliWebsocketServer(circuits.BaseComponent):
instance = None
def __init__(self):
circuits.BaseComponent.__init__(self)
self.sock = eventlet.listen(('0.0.0.0', 9999))
self.server = async_eventlet_wsgiserver.server(self.sock, handle_clients)
print "websocket server started."
NaaliWebsocketServer.instance = self
self.clientavs = {}
def newclient(self, clientid, position, orientation):
ent = naali.createMeshEntity("Jack.mesh")
ent.placeable.Position = Vec3(position[0], position[1], position[2])
ent.placeable.Orientation = Quat(mathutils.euler_to_quat(orientation))
print "New entity for web socket presence at", ent.placeable.Position
self.clientavs[clientid] = ent
def updateclient(self, clientid, position, orientation):
ent = self.clientavs[clientid]
ent.placeable.Position = Vec3(position[0], position[1], position[2])
ent.placeable.Orientation = Quat(mathutils.euler_to_quat(orientation))
@circuits.handler("update")
def update(self, t):
if self.server is not None:
self.server.next()
@circuits.handler("on_exit")
def on_exit(self):
# Need to figure something out what to do and how
pass
clients = set()
def sendAll(data):
for client in clients:
client.send(json.dumps(data))
@websocket.WebSocketWSGI
def handle_clients(ws):
print 'START', ws
myid = random.randrange(1,10000)
clients.add(ws)
scene = naali.getScene("World")
while True:
# "main loop" for the server. When your done with the
# connection break from the loop. It is important to remove
# the socket from clients set
try:
msg = ws.wait()
except socket.error:
#if there is an error we simply quit by exiting the
#handler. Eventlet should close the socket etc.
break
print msg
if msg is None:
# if there is no message the client will quit.
break
try:
function, params = json.loads(msg)
except ValueError, error:
print error
continue
if function == 'CONNECTED':
ws.send(json.dumps(['initGraffa', {}]))
x, y, z = SPAWNPOS[0], SPAWNPOS[1], SPAWNPOS[2]
start_position = (x, y, z)
start_orientation = (1.57, 0, 0)
NaaliWebsocketServer.instance.newclient(myid, start_position, start_orientation)
ws.send(json.dumps(['setId', {'id': myid}]))
sendAll(['addEntity', {'id': myid}])
sendAll(['addComponent', {'id': myid, 'component': 'EC_Mesh', 'url': 'http://localhost:8000/WebNaali/ankka.dae'}])
sendAll(['addComponent',{'id': myid, 'component': 'EC_Placeable',
'x': x,
'y': y,
'z': z,
'rotx': 1.57,
'roty': 0,
'rotz': 0}])
# ents = scene.GetEntitiesWithComponentRaw("EC_DynamicComponent")
# for ent in ents:
# id = ent.Id
# position = ent.placeable.Position.x(), ent.placeable.Position.y(), ent.placeable.Position.z()
# orientation = mathutils.quat_to_euler(ent.placeable.Orientation)
# sendAll(['addObject', {'id': id, 'position': position, 'orientation': orientation, 'xml': scene.GetEntityXml(ent).data()}])
elif function == 'Naps':
ws.send(json.dumps(['logMessage', {'message': 'Naps itelles!'}]))
elif function == 'setAttr':
id = params.get('id')
component = params.get('component')
if component == 'EC_Placeable':
# What to do here?
ent = NaaliWebsocketServer.instance.clientavs[myid]
position = ent.placeable.Position
orientation = mathutils.quat_to_euler(ent.placeable.Orientation)
x = params.get('x', position.x())
y = params.get('y', position.y())
z = params.get('z', position.z())
rotx = params.get('rotx', orientation[0])
roty = params.get('roty', orientation[1])
rotz = params.get('rotz', orientation[2])
11
NaaliWebsocketServer.instance.updateclient(myid, (x, y, z), (rotx, roty, rotz))
ents = scene.GetEntitiesWithComponentRaw("EC_OpenSimPresence")
for ent in ents:
x = ent.placeable.Position.x()
y = ent.placeable.Position.y()
z = ent.placeable.Position.z()
orientation = mathutils.quat_to_euler(ent.placeable.Orientation)
id = ent.Id
sendAll(['addEntity', {'id': id}])
sendAll(['addComponent', {'id': id, 'component': 'EC_Mesh', 'url': 'http://localhost:8000/WebNaali/ankka.dae'}])
sendAll(['addComponent', {'id': id, 'component': 'EC_Placeable'}])
sendAll(['setAttr',
{'id': id,
'component': 'EC_Placeable',
'x': x,
'y': y,
'z': z,
'rotx': orientation[0],
'roty': orientation[1],
'rotz': orientation[2]}])
elif function == 'updateObject':
id = params['id']
data = params['data']
entity = scene.GetEntityRaw(id)
component = entity.GetComponentRaw('EC_DynamicComponent', 'door')
component.SetAttribute('opened', data['opened'])
elif function == 'setSize':
y_max = params['height']
x_max = params['width']
elif function == 'reboot':
break
clients.remove(ws)
print 'END', ws