使用servlet打破连接 [英] smack connection using servlets

查看:145
本文介绍了使用servlet打破连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我使用smack和servlets连接到Facebook,我可以发送和接收聊天消息。
但是这里连接对象是实例变量(不是线程安全的),所以所有用户都获得相同的连接对象。

Here I am connecting to facebook using smack and servlets ,I can able to send and receive chat messages. But here connection object is instance variable(not thread-safe),so all the users are getting same connection object.

如果我们在doGet内部声明XMPPConnection对象()方法,当用户发送聊天消息时,每次
都要连接。

If we declare XMPPConnection object inside doGet() method we have to take connection every time when the user send chat message.

为我的问题提供一些解决方案。

provide some solution for my problem.

public class Home_page_action extends HttpServlet  implements MessageListener{

 public XMPPConnection connection;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

               if(connection!=null)
               {connection.connect("uname","password");}
               else{
               //send message code to target user
                }


              }

}


推荐答案

你的连接池,每次你只需要调用该类的getInstance。

Make some class to maintain pool of your connection and every time you have to just call getInstance of that class..

这篇关于使用servlet打破连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆