1

ログインしたクライアント側のユーザー名を、クライアントが入力したメッセージとともに websocket スクリプトに送信したいと思います。メッセージとともに送信される別の変数 rge ユーザー名の実装に関するサポートが必要です。今私が使用している: https://github.com/Flynsarmy/PHPWebSocket-Chatと js は

Server.send( 'message', text );

Server は FancyWebSocket です。これは次の宛先に送信されます。

var FancyWebSocket = function(url)
{
    var callbacks = {};
    var ws_url = url;
    var conn;

    this.bind = function(event_name, callback){
        callbacks[event_name] = callbacks[event_name] || [];
        callbacks[event_name].push(callback);
        return this;// chainable
    };

    this.send = function(event_name, event_data){
        this.conn.send( event_data );
        return this;
    };

    this.connect = function() {
        if ( typeof(MozWebSocket) == 'function' )
            this.conn = new MozWebSocket(url);
        else
            this.conn = new WebSocket(url);

        // dispatch to the right handlers
        this.conn.onmessage = function(evt){
            dispatch('message', evt.data);
        };

        this.conn.onclose = function(){dispatch('close',null)}
        this.conn.onopen = function(){dispatch('open',null)}
    };

    this.disconnect = function() {
        this.conn.close();
    };

    var dispatch = function(event_name, message){
        var chain = callbacks[event_name];
        if(typeof chain == 'undefined') return; // no callbacks for this event
        for(var i = 0; i < chain.length; i++){
            chain[i]( message )
        }
    }
};

私のserver.phpに次のようにバインドされています:

$Server->bind('message', 'wsOnMessage');    
function wsOnMessage($clientID, $message, $messageLength, $binary)
4

1 に答える 1

0

どのコンテンツを送信するかはあなた次第です。たとえば、次のように送信できます

Server.send("USERNAME|message", text);

|次に、受信した文字列をパイプ記号で分割することにより、サーバー上のユーザー名を抽出できます。

于 2013-03-13T10:24:46.550 に答える