Question

I'm trying to create a typical chat room app in Google App Engine. So far, when the user logs in, I'm able to create a token for them, which is being displayed in the chat area.

My problem is that after getting this token I'm not able to open or use the channel.

This is my JavaScript code below, where I'm able to create the token by sending the user-entered clientid and sending it to the servlet (chatroom.java):

 <script type="text/javascript">
 $(document).ready(function(){

     alert("doc");

     $("#field1").hide();

     $(".button").click(function(){
         $("#field2").hide();
         $("#field1").fadeIn(2500);
         var clientid = $("#textbox2").val();
         var form=$('#form1');

         $.get(form.attr('action'),$(form1).serialize(),function(data,status){

             alert(status);       
             $('#display').val(" client id is "+clientid);
             $('#display').val(" tok id is "+data.token);
        });
     });
 });
 </script>

This is my servlet code below, where I'm able to create the token:

package com.example.chatroom;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.*;

import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

@SuppressWarnings("serial")
public class ChatroomServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException {

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        try
        {
            String clientid = request.getParameter("clientid");
            ChannelService channelService = ChannelServiceFactory.getChannelService();

            String token = channelService.createChannel(clientid);

            System.out.println("token is ="+token);

            JSONObject job=new JSONObject();
            job.put("token",token);

            String jsondata=job.toString();

            out.write(jsondata);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

How do I open this channel and use it to display a "connection established" message? I need a little example for working with the channel API.

Was it helpful?

Solution

use function onOpened() in your javascript to print that the client is connected.

function onOpened() 
{

// event handler from when Connection has established.

                    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top