prayeri137
prayeri137
采纳率100%
2017-05-25 02:35 阅读 1.4k
已采纳

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

50

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

8条回答 默认 最新

  • 已采纳
    qq_27718453 庄粟 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
    把这三个文件弄好就能用了

    点赞 评论 复制链接分享
  • playboyanta123 show_code 2017-05-25 03:08
  • playboyanta123 show_code 2017-05-25 03:09
  • qq_23126581 郭老师的小迷弟雅思莫了 2017-05-25 03:09

    连接不上数据库,那你有这个数据库吗?

    点赞 评论 复制链接分享
  • qq_27718453 庄粟 2017-05-25 03:16
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    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">
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
    <servlet-name>wsServlet</servlet-name>
    <servlet-class>websocket.DemoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>wsServlet</servlet-name>
    <url-pattern>/wsServlet</url-pattern>
    </servlet-mapping>
    </web-app>
    
    点赞 评论 复制链接分享
  • qq_27718453 庄粟 2017-05-25 03:18
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=UTF-8>
    <title>Tomcat WebSocket Chat</title>
    <script>
    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();
    }
    </script>
    </head>
    <body>
    <textarea id="chatlog" readonly></textarea><br/>
    <input id="msg" type="text" />
    <button type="submit" id="sendButton" onClick="postToServer()">Send!</button>
    <button type="submit" id="sendButton" onClick="closeConnect()">End</button>
    </body>
    </html>
    

    不好意思啊有点乱

    点赞 评论 复制链接分享
  • hgq0916 奔腾的野马 2017-05-26 15:19

    图片说明
    你这个项目我已经调试好了,这是运行结果,如果需要我发给你,不过你得按照我的配置去做才能成功

    点赞 评论 复制链接分享
  • Lie_with Lie_with 2019-03-22 14:12

    大佬求源码 邮箱jzeran@163.com

    点赞 评论 复制链接分享

相关推荐