prayeri137 2017-05-25 02:35 采纳率: 100%
浏览 1456
已采纳

谁能提供一个能用的JAVA的Websocket聊天室

在下载频道下载了N个都不能用,项目能跑起来却提示连接不上,在此求各位dalao给一个能用的JAVA的Websocket聊天室,或者指导下这个项目为啥能跑起来却连接不上http://download.csdn.net/download/wendysunday123/9788097

在线等,挺急的,今天解答的后面追加悬赏50

  • 写回答

9条回答 默认 最新

  • 庄粟 2017-05-25 03:11
    关注

    ---------------------------------------------------------------------------java :DemoServletTwo
    package websocket;

    import java.io.IOException;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;

    import java.util.concurrent.CopyOnWriteArraySet;

    import java.util.concurrent.atomic.AtomicInteger;

    import javax.websocket.MessageHandler;
    import javax.websocket.OnClose;

    import javax.websocket.OnError;

    import javax.websocket.OnMessage;

    import javax.websocket.OnOpen;

    import javax.websocket.Session;

    import javax.websocket.server.ServerEndpoint;

    import entity.Member;

    @ServerEndpoint(value = "/websocket/chat")

    public class DemoServletTwo {

    //private static final Log log = LogFactory.getLog(ChatAnnotation.class);

    private static final String GUEST_PREFIX = "Guest";

    private static final AtomicInteger connectionIds = new AtomicInteger(0);

    private static final Set connections = new CopyOnWriteArraySet();

    private final Member member;

    private final String nickname;

    private Session session;

    public DemoServletTwo() {

    member = new Member();
    nickname = GUEST_PREFIX + connectionIds.getAndIncrement();

    }

    //建立连接

    @OnOpen

    public void start(Session session) {

    this.session = session;

    connections.add(this);
    Map param = session.getPathParameters();
    if(param.get("name") != null){
    member.setName(param.get("name"));
    }
    if(param.get("sex") != null){
    member.setSex(param.get("sex"));
    }

      String message = String.format("* %s %s", nickname, "has joined.");  
      System.out.println(message);  
    

    }

    //接受消息

    @OnMessage

    public void incoming(String message) {

    System.out.println(message.toString());

    //broadcast(filteredMessage);

    broadcast(member.talk(message.toString()));

    }

    //客户端关闭了连接

    @OnClose

    public void end() {

    connections.remove(this);

    String message = String.format("* %s %s", nickname, "has disconnected.");

    System.out.println(message);

    //broadcast(message);

    }

    //WebSocket服务出错

    @OnError

    public void onError(Throwable t) throws Throwable {

    //log.error("Chat Error: " + t.toString(), t);

    }

    private static void broadcast(String msg) {

    for (DemoServletTwo client : connections) {

    try {

    synchronized (client) {

    client.session.getBasicRemote().sendText(msg);

    }

    } catch (IOException e) {

    //log.debug("Chat Error: Failed to send message to client", e);

    connections.remove(client);

    try {

    client.session.close();

    } catch (IOException e1) {

    // Ignore

    }

    String message = String.format("* %s %s",

    client.nickname, "has been disconnected.");

    broadcast(message);

    }

    }

    }

    }

    --------------------------------------------------------------------------------------------config: web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    Archetype Created Web Application

    wsServlet
    websocket.DemoServlet


    wsServlet
    /wsServlet


    ------------------------------------------------------------------------------chat.html
    <!DOCTYPE html>



    Tomcat WebSocket Chat var ws = new WebSocket("ws://localhost:8080/websocket//websocket/chat"); ws.onopen = function(){ }; ws.onmessage = function(message){ document.getElementById("chatlog").textContent += message.data + "\n"; }; function postToServer(){ ws.send(document.getElementById("msg").value); document.getElementById("msg").value = ""; } function closeConnect(){ ws.close(); }






    Send!
    End

    创建个项目名为:websocket
    把这三个文件弄好就能用了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥20 请问这种量表怎么用spss量化分析(作为中介模型的因变量
  • ¥55 AD844 howland电流源如何驱动大额负载
  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入