Prakash G
Prakash G

Reputation: 35

Cannot get the extra argument value on cfwebsocket JS function publish

I am trying to create the client to client chat application by using cfwebsocket. I referenced the adobe example. In that example we pass an extra argument on to the publish function. So I passed the receiver id to the publish function, but cannot get that value in the msgHandler function.

<cfoutput>
    <cfif !structkeyexists(session,'userName')>
        <cflocation url="index.cfm?msg=Please login first" addtoken="false">
    </cfif>
    <cfdump var="i am chat.cfm" />
    <a href="logout.cfm" style="float:right">Logout</a>
    <cfwebsocket name="myworld" onMessage="msgHandler" onOpen="openHandler"/>

    <script>
        var msgHandler = function(message){
            // Get data from the recieved message token
            var data = message.data;
            console.log(message.data.to);
            if(data){
                // If data is present write it to the div
                var txt=document.getElementById("myDiv");
                txt.innerHTML+= data + "<br>";
            }
        }

        var sayHello = function(){
            uname = document.getElementById("username").value;
            receiver = document.getElementById("selectUser").value;
            //var myData = {publishedBy: ''+uname, receiver:''+receiver}
            // Calling authenticate from client side. Calling this
            //function will invoke onWSAuthenticate from Application.cfc

            myworld.authenticate(uname,"password");
            myworld.subscribe("chat");
            // Client says Hello World
            myworld.publish("chat","Hello World! WebSocket is here !!",{to:receiver});
        }

        var openHandler = function(){
            // do nothing
        }
    </script>
    <input type="hidden" name="userName" id="username" value="#session.userName#">
    <input  id="hello" type="button" value="Say Hello!" onclick="sayHello();">

    <div id="myDiv"></div>

    <cfset users = Application.usersDAO.read()>
    <select name="user" id="selectUser">
        <option value="0">Select User</option>
        <cfloop query="users">
            <option value="#id#">#username#</option>
        </cfloop>
    </select>   
</cfoutput>

Upvotes: 1

Views: 72

Answers (1)

cfprabhu
cfprabhu

Reputation: 5362

You should pass your message and extra argument like a json object.

var sayHello = function()
		{
			uname = document.getElementById("username").value;
			userID = document.getElementById("userID").value;
			receiverID = document.getElementById("ToUser").value;
			receiverName = document.getElementById("ToUserName").value;
			// Calling authenticate from client side. Calling this function will invoke onWSAuthenticate from Application.cfc
			myworld.authenticate(uname,"password");
			// Client says Hello World

			// console.log($('#input').val())
			msg = {'username': uname, 'userID' : userID, 'chat': $('#input').val().trim(), 'to':receiverID, 'receiverName' : receiverName };
			myworld.publish("world",msg);
			$('#input').val('');
		}

In msgHandler function you will get message.data.to and message.data.userId. You will store the userId on session scope inside the msgHandler you will compare the session userID and message.data.to. If both are same you will appeand your message on your chat window.

Upvotes: 0

Related Questions