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条)

报告相同问题?

悬赏问题

  • ¥15 关于推送项目到github的问题
  • ¥15 急!C++指针编写相关的问题
  • ¥30 用python写一个多签情况下波场的代理资源和回收资源
  • ¥15 怎么在matlab中输出显示泵的流量-扬程和管路损失与流量均在一个表格里
  • ¥15 matlab学期例题代码答疑
  • ¥50 Hadoop:好友推荐
  • ¥15 在线手电筒追加按钮JS
  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?