sendMessage
accepts a byte string or a unicode string - not a dictionary. This is because WebSockets are a transport for binary data and text data. It is not a transport for structured objects.
You can send the JSON encoded form of the dictionary but you cannot send the dictionary itself:
def broadcast(self):
response = urllib2.urlopen('http://localhost:8001/json?as_text=1')
for c in self.clients:
c.sendMessage(response)
Though note that you will actually want to use twisted.web.client
- not the blocking urllib2
:
from twisted.internet import reactor
from twisted.web.client import Agent, readBody
agent = Agent(reactor)
def broadcast(self):
getting = agent.request(
b"GET", b"http://localhost:8001/json?as_text=1")
getting.addCallback(readBody)
def got(body):
for c in self.clients:
c.sendMessage(body)
getting.addCallback(got)
return getting