使用Tomcat 8 Web应用程序的错误代码404

时间:2016-12-21 18:58:56

标签: java websocket struts2 tomcat8

我正在尝试使用Tomcat 8.0.28(不是Tomcat 7)和Struts 2来实现Websocket。但是当我尝试从JavaScript连接到它时,我得到了HTTP ERROR 404。我已经检查过,如果WEB-INF/lib文件夹中有任何tomcat-apis,但没有。

这是我的Websocket代码:

package de.whs.ms.struts.websocket;

import de.whs.ms.services.Log;
import de.whs.ms.services.TinsServer;
import de.whs.ms.struts.websocket.coder.RFIDDecoder;
import de.whs.ms.struts.websocket.coder.RFIDEncoder;
import models.RfidDevice;
import net.ThirdIndustries.Exceptions.InvalidTypeException;
import net.ThirdIndustries.KSON.KSONObject;
import net.ThirdIndustries.NetworkProtocol.MessageMeta;
import net.ThirdIndustries.NetworkProtocol.NetworkProtocol;
import net.ThirdIndustries.NetworkProtocol.iNetworkMeta;
import net.ThirdIndustries.TINSServer.IMessageRecievedListener;
import net.ThirdIndustries.TINSServer.TINSClientServer;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
* Created by Maren on 15.12.16.
*/

@ServerEndpoint(value="/websocket", encoders = RFIDEncoder.class, decoders = RFIDDecoder.class)
public class Websocket implements IMessageRecievedListener {

    private RfidDevice rfidDevice;

    private Set<Session> userSessions = Collections.synchronizedSet(new HashSet<>());

    @OnOpen
    public void onOpen(Session userSession){
        System.out.println("< ----------- ON OPEN ----------- >");

        if(!userSessions.contains(userSession)){
            userSessions.add(userSession);
        }
    }

    @OnMessage
    public void onMessage(String message, Session session){
        System.out.println("< ----------- ON MESSAGE ----------- >");
        System.out.println("Client sendet: " + session);
        System.out.println("Message empfangen: " + message);

        try {
            session.getBasicRemote().sendObject(rfidDevice);
        } catch (IOException | EncodeException ex) {
            ex.printStackTrace();
        }
    }

    @OnClose
    public void onClose(Session session){
        System.out.println("< ----------- ON CLOSE ----------- >");
        userSessions.remove(session);
    }
}

这是我的Javascript-File代码:

<script type="text/javascript">
    var websocket;
    var messages = document.getElementById("messages");

    function openSocket(){
        if(websocket !== undefined && websocket.readyState !== WebSocket.CLOSED){
            writeResponse("Websocket is already opened");
            return;
        }

        //Create a new instance of websocket
        websocket = new WebSocket("ws://localhost:8080/ms/websocket");

        //Binds functions to the listeners for the websocket
        websocket.onopen = function(event){
            if(event.data === undefined)
                return;
            writeResponse(event.data);
        };

        websocket.onmessage = function(event){
            writeResponse(event.data);
        };

        websocket.onclose = function(event){
            writeResponse("Connection closed");
        };
    }

    function send(){
        var text = "balalalala";
        websocket.send(text);
    }

    function closeSocket(){
        websocket.close();
    }

    function writeResponse(text){
        messages.innerHTML = "Message empfangen: " + text;
    }
</script>

此外,我已从struts中排除了“* / websocket”:

<constant name="struts.action.excludePattern" value="/websocket.*?"/>
<constant name="struts.action.excludePattern" value="^ws://.+$"/>

仍然不起作用。

0 个答案:

没有答案