On Github Legoben / Socket-Presentation
Messages:
var ws = new WebSocket("ws://localhost:9008/ws"); ws.onopen = function () { // Web Socket is connected, send data using send() }; ws.onmessage = function (evt) { json = JSON.parse(evt.data); var m = document.getElementById('messages') m.innerHTML = 'user: ' +json.chat + '' + m.innerHTML }; function sendMessage(){ if(document.getElementById('sendmessage').value == ''){ return; } var msg = document.getElementById('sendmessage').value ws.send('{"event":"sendmessage", "chat":"'+msg+'"}') document.getElementById('sendmessage').value = ''; }
users = [] def open(self): users.append def on_message(self, message): for user in users: user.write_message(message) def on_close(self): if self in users: users.remove(self)