javascript – 扭曲的websocket chatserver开放认证

前端之家收集整理的这篇文章主要介绍了javascript – 扭曲的websocket chatserver开放认证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 python chatserver,它使用扭曲和高速缓存的websockets进行连接.
factory = MessageServerFactory("ws://localhost:9000",debug=debug,debugCodePaths=debug)
factory.protocol = MessageServerProtocol
factory.setProtocolOptions(allowHixie76=True)
listenWS(factory)

这是服务器

import logging
from autobahn.websocket import WebSocketServerFactory,WebSocketServerProtocol
from DatabaseConnector import DbConnector
from LoginManager import LoginManager
from MessageTypes import MessageParser

class MessageServerProtocol(WebSocketServerProtocol):
def onOpen(self):
    self.factory.register(self)

def onMessage(self,msg,binary):
    if not binary:
        self.factory.processMessage(self,msg)

def connectionLost(self,reason):
    WebSocketServerProtocol.connectionLost(self,reason)
    self.factory.unregister(self)


class MessageServerFactory(WebSocketServerFactory):

logging.basicConfig(filename='log/dastan.log',format='%(levelname)s:%(message)s',level=logging.WARNING)

def __init__(self,url,debug=False,debugCodePaths=False):
    WebSocketServerFactory.__init__(self,debugCodePaths=debugCodePaths)
    self.clients = {}
    self.connector = DbConnector()
    self.messages = MessageParser()
    self.manager = LoginManager()

def register(self,client):
    print "%s connected" % client.peerstr

def unregister(self,client):
    if self.clients.has_key(client):
        self.processlogout(client)
    print "%s disconnected" % client.peerstr

def processMessage(self,client,msg):
    try:
        msg = self.messages.parseMessage(msg)
        action = msg['Type']
    except ValueError,e:
        logging.warning("[Parse]:%s",e.message)
        client.sendMessage(self.messages.createErrorMessage("could not parse your message"))
        return

    if action == "ChatMessage":
        self.processChatMessage(client,msg)
    # elif action == "Login":
    #   self.processLogin(client,msg)
    # elif action == "logout":
    #   self.processlogout(client)
    elif action == "OpenId":
        self.manager.processLogin(client,msg)


def processChatMessage(self,msg):
    if not self.clients.has_key(client):
        client.sendMessage(self.messages.createErrorMessage('Not authorized'))
        return

    if not msg['Message']:
        client.sendMessage(self.messages.createErrorMessage('Invalid Message'))
        return

    if not msg['Recipient']:
        client.sendMessage(self.messages.createErrorMessage('Invalid Recipient'))
        return

    if msg['Recipient'] in self.clients.values():
        for c in self.clients:
            if self.clients[msg['Recipient']]:
                c.sendMessage(self.messages.chatMessage(msg['Sender'],msg['Message']))
                print "sent message from %s to %s: '%s' .." % (msg['Sender'],msg['Recipient'],msg['Message'])
    else:
        client.sendMessage(self.messages.createErrorMessage('User not registered'))

def checkSender(self,user,client):
    if user in self.clients.values() and self.clients[client] == user:
        return
    else:
        self.clients[client] = user

独立的html / js客户端可以连接和发送聊天消息.但是,在打开websocket之前,我想实现一个开放的身份验证(由服务器执行).

这是onload功能

var wsuri = "ws://192.168.0.12:9000";

if ("WebSocket" in window) {
    sock = new WebSocket(wsuri);
} else if ("MozWebSocket" in window) {
    sock = new MozWebSocket(wsuri);
} else {
    log("Browser does not support WebSocket!");
    window.location = "http://autobahn.ws/unsupportedbrowser";
}

if (sock) {
    sock.onopen = function () {
        log("Connected to " + wsuri);
    }

    sock.onclose = function (e) {
        log("Connection closed (wasClean = " + e.wasClean + ",code = " + e.code + ",reason = '" + e.reason + "')");
        sock = null;
    }

    sock.onmessage = function (e) {
        receive(e.data);
    }
}

因为我是新的python /扭曲我不知道如何做到这一点,例子大多只显示websocket聊天室没有验证.

如何正确实现openid?因为它还需要重定向,这将打破ws连接.

解决方法

重定向之前无法打开ws.打开它,然后你的代码应该工作.

祝你好运.

通过这个方式,谷歌在它的主页上这样做.

猜你在找的JavaScript相关文章