1

cfwebsocket を使用して Facebook のようなチャット アプリケーションを開発するにはどうすればよいでしょうか。ユーザーが入力したメッセージをサーバーに送信する方法と、そのメッセージをサーバーから特定のクライアントに送信する方法についての説明はありません。

<script type="text/javascript"> 
       function mymessagehandler(aevent, atoken) 
       { 
        console.log(aevent);
        console.log(atoken);
           var message = aevent.msg; 
           var txt=document.getElementById("myDiv"); 
           txt.innerHTML = txt.innerHTML + message  +"<br>"; 
       } 
</script> 
<cfwebsocket name="mycfwebsocketobject" onmessage="mymessagehandler" subscribeto="stocks" > 
<cfdiv id="myDiv"></cfdiv>

上記のコードは、ディスプレイに ok を出力するだけです。株式オブジェクト内でメッセージを渡す方法がわかりません。誰でもこれについて助けることができますか?前もって感謝します

これは私が使用している株式アプリケーションです

this.wschannels =      [ {name="stocks",        cfclistener="myChannelListener" }];
4

1 に答える 1

1

これは、チャットアプリケーションを機能させるために私が行ったことです

チャットアプリです

<cfwebsocket name="ChatSocket" onOpen="openHandler" onMessage="msgHandler" onError="errHandler">

これがスクリプトです

function openHandler(){
    //Subscribe to the channel, pass in headers for filtering later
    ChatSocket.subscribe('chatChannel',{name: 'TheUserName', UserID: 'TheUserID', AccountID: 'AnUniqueID' });
}

// function to send the message. we can call this when the user clicks on send message
function publish(userID){
    var msg = {
        AccountID: "AnUniqueID",
        publisher: userID,
        id: userID,
        message: document.getElementById("Name").value + " : " + document.getElementById("message").value
    };
    //When including headers, the "selector" is where you will filter who it goes to.
    var headers = {
        AccountID: "AnUniqueID",
        publisher: userID, 
        id: userID
    };
    // we can save the chat history by an ajax call here

    ChatSocket.publish('chatChannel',msg, headers);

}

// this is the receiving function
function msgHandler(message){
// if condition to display the message to the user who are sending and receiving
    if(message.data !== undefined && message.data.message !== undefined && (message.data.id == '#session.userID#' || message.data.publisher == '#session.userID#')) { 
        var data = message.data.message;
        console.log(data);
        //showing the message
        var txt=document.getElementById("myDiv");
        txt.innerHTML+= data + "<br>"; 
    } 
}

function errHandler(err){
    console.log('err');
    console.log(err);
}
于 2015-07-15T06:48:18.990 に答える