Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

前端访问问题 #234

Open
f-cs opened this issue Jul 7, 2022 · 10 comments
Open

前端访问问题 #234

f-cs opened this issue Jul 7, 2022 · 10 comments

Comments

@f-cs
Copy link

f-cs commented Jul 7, 2022

大佬,我前端是基于socketJs连接,会请求ws/info接口,报错如下:

GET http://localhost:8888/ws/info?t=1657192185347 403 (Forbidden)

希望可以给个前端连接的例子做参考

前端连接代码:

`<script>
var stompClient = null;

function connect() {
    // 连接上服务端点
    var socket = new SockJS("http://localhost:8888/ws");
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        var id = $("#tt").val();
        console.log("我是输入的id=" + id)
        // 订阅
        stompClient.subscribe('/' + id, function (body) {
            // 转json输出
            console.log("服务端推送数据:", JSON.parse(body.body));
            let element = document.getElementById("ws-data");
            element.innerHTML = body.body
        });
    });
}

$(function () {
    $("#connect").click(function (e) {
        connect();
        e.preventDefault();
    });
});

</script>`

后端代码:

`
@slf4j
@component
@serverendpoint(path = "/ws/{arg}", host = "localhost", port = "8888", corsOrigins = "/ws/*", corsAllowCredentials = "true")

public class NettyWebSocket {
.........
}`

@YeautyYE
Copy link
Owner

YeautyYE commented Jul 7, 2022

改一下 path = "/ws"

@f-cs
Copy link
Author

f-cs commented Jul 7, 2022

改了后报错:
GET http://localhost:8888/ws/info?t=1657194321454 404 (Not Found)
有没有个例子让我参考下

@YeautyYE
Copy link
Owner

YeautyYE commented Jul 7, 2022

改了后报错: GET http://localhost:8888/ws/info?t=1657194321454 404 (Not Found) 有没有个例子让我参考下

https://my.oschina.net/u/3580577/blog/2088114

@f-cs
Copy link
Author

f-cs commented Jul 7, 2022

老大,上面的例子针对老版本(0.6.3)可以使用,新版本(0.12.0)貌似不行,我再自己研究下吧

@f-cs
Copy link
Author

f-cs commented Jul 7, 2022

老大,我找到原因了,@BeforeHandshake鉴权问题,谢了

@YeautyYE
Copy link
Owner

YeautyYE commented Jul 7, 2022

老大,我找到原因了,@BeforeHandshake鉴权问题,谢了

ok

@yangmingf
Copy link

老大,我找到原因了,@BeforeHandshake鉴权问题,谢了

你好,这个问题是怎么解决的呢? 我这边也遇到了,在本地是正常的。部署到服务器不正常。 然后没有使用@BeforeHandshake

@zwb993
Copy link

zwb993 commented Jul 21, 2023

同问 @YeautyYE @f-cs 如何解决的?还是报错404

@aazainn
Copy link

aazainn commented Jul 25, 2023

我也遇到这个了,奇怪的是之前我一模一样的写法是可以的。不知道跟我后面升级jdk有没有关系……
解决办法就是,把url上的动态参数去掉……
path = "/ws/{arg}"
这种写成具体的,比如
path = "/ws/info"

@aazainn
Copy link

aazainn commented Jul 25, 2023

同问 @YeautyYE @f-cs 如何解决的?还是报错404
@zwb993
把url上的动态参数去掉……
path = "/ws/{arg}"
这种写成具体的,比如
path = "/ws/info"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants