0

I am building a realtime web application.

I want just to connect the client, identify and send a different message to each from the server, using sockjs

What we achieved so far is connect from the browser and send a message that I answered the same server.

current code

server.py

# -*- coding: utf-8 -*-
from sockjs.tornado import SockJSRouter, SockJSConnection
from tornado import web, ioloop

class ConnectionHandler(SockJSConnection):

    def on_open(self, info):
        print 'new connection'

    def on_message(self, msg):
        print str("server receives: %s" % msg)
        self.send(u"server responds: %s" % msg)

if __name__ == "__main__":
    onopen = SockJSRouter(ConnectionHandler, r"/websocket")

    application = web.Application(onopen.urls)

    application.listen(8686)
    ioloop.IOLoop.instance().start()

client.html

<!DOCTYPE html>
<html>
    <head>
        <title>SockJS</title>
    </head>
    <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
    <script>
        con = new SockJS('http://localhost:8686/websocket');
        con.onmessage = function(evt){
            x = document.createElement("p");
            x.innerHTML = evt.data;
            document.getElementById("msgbox").appendChild(x);
        }
        function DispatchText(){
            var userInput = document.getElementById("message").value;
            document.getElementById("message").value = "";
            x = document.createElement("p");
            x.innerHTML = "message sent: " + userInput;
            document.getElementById("msgbox").appendChild(x);
            con.send(userInput);
        }
    </script>
    <body>
        <p style="width: 800px">Use form to communicate with the server.</p>
        <div id="msgbox" style="font-size: 14pt; height: 500px; width: 800px; overflow: scroll; border: 1px solid black"></div>
        <form id="communication" onsubmit="DispatchText()" action="javascript:void(0);">
            <input type="text" id="message" name="message" autocomplete="off" style="width:700px" />
            <input type="submit" id="sub" name="sub" value="send" style="width:90px" />
        </form>
    </body>
</html>

problems

how to identify each client?

as sending a message to a single client, of all who are connected?

user987055
  • 1,039
  • 4
  • 14
  • 25

1 Answers1

1

You need to keep a list of connections. Now when a message is received it is sent to all connected clients:

class ConnectionHandler(SockJSConnection):
    connections = set()

    def on_open(self, info):
        print 'new connection'
        self.connections.add(self)

    def on_message(self, msg):
        print str("server receives: %s" % msg)

        for conn in self.connections:
            conn.send(u"server responds: %s" % msg)

        # Note you could also use self.broadcast(self.connections, msg)

    def on_close(self):
        self.connections.remove(self)
koblas
  • 25,410
  • 6
  • 39
  • 49
  • ok, and for a single customer? you can select? can be drawn from that list? or I have to send a custom id from the browser? more or less as it would be? – user987055 Nov 21 '12 at 23:44
  • To identify a single client - well, you need to have something identifiable. Maybe a user name? maybe a cookie? Or else - just assign a random id to every client. It's up to you to maintain the list of connections and to deal with them. – Marek Nov 22 '12 at 11:50
  • Remember - you are iterating across ConnectionHandler objects, so any additional data you add to the object can be used in a conditional to determine whom to send to. – koblas Nov 22 '12 at 15:20