连接到WebSocket失败:意外的响应代码:404 [英] Connection to WebSocket Failed: unexpected response code : 404

查看:234
本文介绍了连接到WebSocket失败:意外的响应代码:404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iam试图与javax.websocket创建聊天。
我正在使用grails。 3.0



这是我的控制器

 包聊天

import javax.websocket.server.ServerEndpoint;
import javax.websocket.OnMessage;

@ServerEndpoint(/ echo)
public class WebsocketHomeController {

def index(){}
$ b $ @OnMessage


public String echo(String incomingMessage){
return我得到了这个(+ incomingMessage +)
+,所以我发回它!
}
}

这是我的index.gsp

 < html> 
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>

< title> Web Socket JavaScript Echo Client< / title>
< script language =javascripttype =text / javascript>
var echo_websocket;
函数init(){
output = document.getElementById(output);

函数send_echo(){
var wsUri =ws://192.168.1.2:8080 / WebsocketHome / echo;
writeToScreen(正在连接+ wsUri);
echo_websocket = new WebSocket(wsUri);
echo_websocket.onopen = function(evt){
writeToScreen(Connected!);
doSend(textID.value);
};
echo_websocket.onmessage = function(evt){
writeToScreen(Received message:+ evt.data);
echo_websocket.close();
};
echo_websocket.onerror = function(evt){
writeToScreen('< span style =color:red;>错误:< / span>'
+ evt.data) ;
echo_websocket.close();
};
}
函数showErrorInfo(e){
alert('Error connecting socket'+ e);
}

函数doSend(message){
echo_websocket.send(message);
writeToScreen(发送消息:+消息);

函数writeToScreen(message){
var pre = document.createElement(p);
pre.style.wordWrap =break-word;
pre.innerHTML = message;
output.appendChild(pre);
}
window.addEventListener(load,init,false);
< / script>
< / head>
< body>
< h1> Echo服务器< / h1>
< div style =text-align:left;>
< form action =>
< input onclick =send_echo()value =按下发送
type =button>
< input id =textIDname =messagevalue =Hello Web Sockets
type =text>
< br>
< / form>
< / div>
< div id =output>< / div>
< / body>
< / html>

这是结果..





我不知道..为什么我得到这个错误...



我试图像这样调试我的代码。

  public String echo(String incomingMessage){
println这是运行
返回我得到了这个(+ incomingMessage +)
+,所以我送回来了!;
}

然后我在控制台日志中获得this is running



我该做什么?

解决方案

在grails 3中添加一个监听器: / p>


  1. 将doWithSpring关闭添加到您的nit / appName / Application.groovy中

https ://github.com/vahidhedayati/testwschatapp/blob/master/grails-app/init/testwschatapp/Application.groovy#L7-L11

  Closure doWithSpring(){
{ - >
myChatConfig DefaultChatConfig




在grails中创建一个文件-app / init / {appname} /DefaultChatConfig.groovy

https://github.com/vahidhedayati/testwschatapp/blob/master/grails-app/init/testwschatapp/DefaultWsChatConfig.groovy

 包myappName 

导入path.to.WebsocketHomeController

导入org.springframework。 boot.context.embedded.ServletContextInitializer
import org.springframework.context.annotation.Bean

import javax.servlet.ServletContext
import javax.servlet.ServletException

class DefaultChatConfig {

@Bean
public ServletContextInitializer myInitializer(){
return new ServletContextInitializer(){
@Override
public void on Startup(ServletContext servletContext)throws ServletException {
ServletContext.addListener(WebsocketHomeController)
}
}
}

}

您需要在第二个文件的顶部导入path.to.WebsocketHomeController

顺便说一句我对聊天插件的评论,我建议你可以制作自己的界面。由此我打算与插件进行交互。如果插件为你做了很多功能,但是让我们说你想管理自己的用户。

您可以在自己的应用程序中编写服务,调用聊天类来查看/编辑/删除等。我已经向您展示了如何将管理员帐户添加到聊天域类通过引导在文档中。



这些事情中的一些需要几年才能完全理解,但如果我想懒惰,我可以从这些域类复制将聊天插件添加到测试项目中,然后使用Grails将它们生成控制器和视图,然后返回到主项目并复制到新控制器/视图中。

如果插件提供功能服务/域类/控制器。他们都可以在您的主应用程序中本地重用。你可以扩展或者如果它的好,那么只需将它返回给插件本身供其他人使用


iam trying to create a chat with javax.websocket. i am using grails. 3.0

this is my controller

package chatting

import javax.websocket.server.ServerEndpoint;
import javax.websocket.OnMessage;

@ServerEndpoint("/echo")
public class WebsocketHomeController {

    def index() { }

    @OnMessage


    public String echo(String incomingMessage) {
            return "I got this (" + incomingMessage + ")"
            + " so I am sending it back !";
        }
    }

this is my index.gsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Web Socket JavaScript Echo Client</title>
    <script language="javascript" type="text/javascript">
        var echo_websocket;
        function init() {
            output = document.getElementById("output");
        }
        function send_echo() {
            var wsUri = "ws://192.168.1.2:8080/WebsocketHome/echo";
            writeToScreen("Connecting to " + wsUri);
            echo_websocket = new WebSocket(wsUri);
            echo_websocket.onopen = function (evt) {
                writeToScreen("Connected !");
                doSend(textID.value);
            };
            echo_websocket.onmessage = function (evt) {
                writeToScreen("Received message: " + evt.data);
                echo_websocket.close();
            };
            echo_websocket.onerror = function (evt) {
                writeToScreen('<span style="color: red;">ERROR:</span> '
                + evt.data);
                echo_websocket.close();
            };
        }
        function showErrorInfo(e) {
            alert('Error connecting socket'+e);
        }

        function doSend(message) {
            echo_websocket.send(message);
            writeToScreen("Sent message: " + message);
        }
        function writeToScreen(message) {
            var pre = document.createElement("p");
            pre.style.wordWrap = "break-word";
            pre.innerHTML = message;
            output.appendChild(pre);
        }
        window.addEventListener("load", init, false);
    </script>
</head>
<body>
    <h1>Echo Server</h1>
    <div style="text-align: left;">
        <form action="">
            <input onclick="send_echo()" value="Press to send"
                   type="button">
            <input id="textID" name="message" value="Hello Web Sockets"
                   type="text">
            <br>
        </form>
    </div>
    <div id="output"></div>
</body>
</html>

this is the result..

i have no idea ..why i get this error...

i tried to debug my code like this..

public String echo(String incomingMessage) {
println "this is runing"
return "I got this (" + incomingMessage + ")"
+ " so I am sending it back !";
}

then i get "this is running" in my console log

what must i do?

解决方案

To add a listener in grails 3:

  1. Add doWithSpring closure to your ìnit/appName/Application.groovy

https://github.com/vahidhedayati/testwschatapp/blob/master/grails-app/init/testwschatapp/Application.groovy#L7-L11

Closure doWithSpring() {
    {->
        myChatConfig DefaultChatConfig
    }
}

Create a file in grails-app/init/{appname}/DefaultChatConfig.groovy

https://github.com/vahidhedayati/testwschatapp/blob/master/grails-app/init/testwschatapp/DefaultWsChatConfig.groovy

package myappName

import path.to.WebsocketHomeController

import org.springframework.boot.context.embedded.ServletContextInitializer
import org.springframework.context.annotation.Bean

import javax.servlet.ServletContext
import javax.servlet.ServletException

class DefaultChatConfig {

    @Bean
    public ServletContextInitializer myInitializer() {
        return new ServletContextInitializer() {
            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                ServletContext.addListener(WebsocketHomeController)
            }
        }
    }

}

You will need to sort out the import line at the top of the 2nd file import path.to.WebsocketHomeController

By the way in one my comments on the chat plugin I suggested you could make your own interfaces. By that I meant to interact with the plugin. If the plugin is doing a lot of the functionality for you but lets say you wish to manage your own users.

You could just write services in your own app that call the chat classes to view/edit/delete etc. The very same way I have shown how you add admin accounts to the chat domain classes via bootstrap in the documentation.

Some of these things takes years to fully better understand but if I wanted to be lazy I could just copy those domain classes from the chat plugin to a test project then using grails get it to generate controllers and views for them then go back to the main project and copy across the new controllers/views.

If a plugin provides functionality services/domain classes / controllers. Their all reusable locally in your main app. You can just expand or if its good then just contribute it back to the plugin itself for others to use

这篇关于连接到WebSocket失败:意外的响应代码:404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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