使用CFWebsocket聊天应用程序

时间:2015-06-15 13:35:59

标签: coldfusion websocket chat cfwebsocket

我们如何使用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>

上面的代码只是在显示屏上打印好了。我不知道如何在stocks对象中传递我的消息。任何人都可以帮忙吗?提前致谢

这是我正在使用的股票申请

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

1 个答案:

答案 0 :(得分: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);
}