Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if payload["server"]:
print("bu mesaj serverdan gelmis demek")
addnewnode(payload["host"])
else:
print(payload["message"])
payload["host"] = ip
payload = json.dumps(payload) #tekrar ÅŸifreleyip server ile paykaÅŸ
self.factory.broadcast(payload)
def connectionLost(self, reason):
WebSocketServerProtocol.connectionLost(self, reason)
self.factory.unregister(self)
clients = []
class BroadcastServerFactory(WebSocketServerFactory):
def __init__(self, url):
WebSocketServerFactory.__init__(self, url)
def register(self, client):
if client not in clients:
print("registered client {}".format(client.peer))
print(clients)
tcp, host, port = client.peer.split(":")
print(host)
clients.append(client)
def unregister(self, client):
if client in clients:
print("unregistered client {}".format(client.peer))
clients.remove(client)
@classmethod
def onConnect(self, request):
print("WebSocket connection request by {}".format(request.peer))
def onOpen(self):
print("WebSocket extensions in use: {}".format(self.websocket_extensions_in_use))
def onMessage(self, payload, isBinary):
self.sendMessage(payload, isBinary)
if __name__ == '__main__':
log.startLogging(sys.stdout)
factory = WebSocketServerFactory(u"ws://127.0.0.1:9000")
factory.protocol = EchoServerProtocol
# Enable WebSocket extension "permessage-deflate".
# Function to accept offers from the client ..
def accept(offers):
for offer in offers:
if isinstance(offer, PerMessageDeflateOffer):
return PerMessageDeflateOfferAccept(offer)
factory.setProtocolOptions(perMessageCompressionAccept=accept)
# run server
listenWS(factory)
webdir = File(".")
url = path_config.get('url', None)
options = path_config.get('options', {})
showServerVersion = options.get('show_server_version', True)
if showServerVersion:
server = "Crossbar/{}".format(crossbar.__version__)
else:
server = "Crossbar"
externalPort = options.get('external_port', None)
protocols = None
headers = None
websocket.WebSocketServerFactory.__init__(self,
reactor=reactor,
url=url,
protocols=protocols,
server=server,
headers=headers,
externalPort=externalPort)
# set WebSocket options
set_websocket_options(self, options)
import autobahn.twisted.websocket as ws
from twisted.python import log
import ckanext.realtime.message_handler as mh
class CkanWebSocketServerFactory(ws.WebSocketServerFactory):
'''Twisted server factory for CKAN WebSocket protocols'''
def __init__(self, url, api_url, apikey, test, debug=False, debugCodePaths=False):
ws.WebSocketServerFactory.__init__(self, url, debug=debug,
debugCodePaths=debugCodePaths)
self.protocol = CkanWebSocketServerProtocol
self.setProtocolOptions(allowHixie76=True)
if test:
# most of the responses in the TestClientMessageHandler are mocked
self.message_handler = mh.TestMessageHandler(api_url, apikey)
else:
self.message_handler = mh.MessageHandler(api_url, apikey)
def listen(self):
'''Listen for incoming WebSocket connections'''
# return a pair with WS protocol spoken (or None for any) and
# custom headers to send in initial WS opening handshake HTTP response
##
return (None, headers)
def onMessage(self, payload, isBinary):
self.sendMessage(payload, isBinary)
if __name__ == '__main__':
log.startLogging(sys.stdout)
headers = {'MyCustomServerHeader': 'Foobar'}
factory = WebSocketServerFactory(u"ws://127.0.0.1:9000")
factory.protocol = EchoServerProtocol
listenWS(factory)
reactor.run()
class EchoServerProtocol(WebSocketServerProtocol):
"""
Example WebSocket server protocol. This is where you define your application
specific protocol and logic.
"""
def onMessage(self, payload, isBinary):
# just echo any WebSocket message received back to client
##
self.sendMessage(payload, isBinary)
class EchoServerFactory(WebSocketServerFactory):
"""
Example WebSocket server factory. This creates new instances of our protocol
for each client connecting.
"""
protocol = EchoServerProtocol
if __name__ == '__main__':
import sys
import argparse
from twisted.python import log
from twisted.internet.endpoints import serverFromString
def __init__(self):
WebSocketServerFactory.__init__(self)
self.clients = {"DASHBOARD": [], "LIBRARY": []}
def __init__(self, url):
WebSocketServerFactory.__init__(self, url)
class BroadcastServerProtocol(WebSocketServerProtocol):
def onOpen(self):
self.factory.register(self)
def connectionLost(self, reason):
WebSocketServerProtocol.connectionLost(self, reason)
self.factory.unregister(self)
def onClose(self, wasClean, code, reason):
_log.info("WebSocket connection closed: {0}".format(reason))
class BroadcastServerFactory(WebSocketServerFactory):
def __init__(self, url):
WebSocketServerFactory.__init__(self, url)
self.clients = []
self.tickcount = 0
def register(self, client):
if client not in self.clients:
_log.info("registered client {}".format(client.peer))
self.clients.append(client)
def unregister(self, client):
if client in self.clients:
_log.info("unregistered client {}".format(client.peer))
self.clients.remove(client)
def __init__(self, queue):
factory=WebSocketServerFactory("ws://localhost:9000", debug=False)
factory.protocol = self.MyServerProtocol
self.queue = queue
lc = task.LoopingCall(self.processSockets)
lc.start(2)
reactor.listenTCP(9000, factory)
reactor.run(installSignalHandlers=0) # no handlers because threads