NutzCN Logo
问答 websocket在远程服务器上报错,有人遇到过吗?
发布于 2613天前 作者 shusheng 2930 次浏览 复制 上一个帖子 下一个帖子
标签:
Generel
Request URL:ws://game.meiph.cn/ssb/websocket
Request Method:GET
Status Code:404 


Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://game.meiph.cn
Access-Control-Expose-Headers:Set-Cookie
Connection:keep-alive
Content-Encoding:gzip
Content-Language:en
Content-Type:text/html;charset=utf-8
Date:Tue, 21 Feb 2017 07:55:40 GMT
Server:nginx
Transfer-Encoding:chunked
X-Powered-By:nutz/1.r.59-SNAPSHOT <nutzam.com>


Request Headers
view source
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:no-cache
Connection:Upgrade
Cookie:sid=plijvjadpihborilfqo5d40p6f
Host:game.meiph.cn
Origin:http://game.meiph.cn
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:tMn0hCbsPTbuTbB2w2hwxA==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
28 回复

是不是把websocket类打包进jar了? 如果是的话, 不会被自动注册的

在远程服务器的lib包下没有看到有websocket的jar包呀

pom.xml文件中为什么有三处对websocket的导入,有什么不同吗?为什么有多个dependencies依赖?

</dependency>
	</dependencies>
	<version>2.9.1</version>
	<dependencyManagement>
		<dependencies>
			<dependency>

用什么打包的?

并不是说websocket api的jar

说的是项目本地的class文件,如果打包进jar了,所声明的websocket endpoint不会被自动扫描

ps,需要tomcat 8才能确保websocket正常

远程使用的是本地编译好的class文件直接运行的

并且在本地的idea上运行都很正常

也是打成war包吗?

没有打包,直接拷贝本地的class文件放到远程上执行的

@qq_97f5068e 很好奇你怎么拷贝的。。。 tomcat?

是的,在tomcat下执行的

idea运行会生成artifacts,下面就 是工程编译后的所有文件,直接拷贝到tomcat下执行

@qq_97f5068e 依然理解不了你是怎么部署的。。。

一般是mvn package得到war文件然后部署到tomcat

如果是war包,该怎么解决这个问题呢?

配置一个artifacts,然后会在 out目录下面生成 web工程文件,我直接把web工程文件,拷贝到 服务器上tomcat的webapp里去了。
然后有更新,就拷贝更新的class文件。

我用的是windows测试机

远程的服务器用了nginx做代理的

在mainsetup.init的末尾加上这句试试

ServerContainer sc = (ServerContainer)nc.getServletContext().getAttribute(ServerContainer.class.getName());
        if (sc != null)
            try {
                sc.addEndpoint(NutzbookWebsocket.class);
            }
            catch (DeploymentException e) {
                
                e.printStackTrace();
            }

这段代码是,在初始化时主动加人NutzbookWebsocket吧,加上去后似乎还是没有效果

是不是nginx没配好, 贴配置看看,需要额外设置的


location / { proxy_http_version 1.1; client_max_body_size 10m; proxy_pass http://nutz; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $remote_addr; proxy_redirect http:// https://; #add_header Access-Control-Allow-Origin "*"; proxy_set_header Upgrade $http_upgrade; // 这个 proxy_set_header Connection $http_connection; // 还有这个 add_header Strict-Transport-Security max-age=63072000; add_header X-Content-Type-Options nosniff; }
server {
        listen       80;
        server_name  baidu.com;
        #charset koi8-r;

        access_log  logs/ssu8.cn.access.log  main;
		location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
			root D:/soft/ssu8/webapps/ROOT;
			expires      30d;
		}
				
		location ~ "^/ssb/user/[^\/]+/[^\/]+" {
               return 605;
        }

		location / {
		proxy_pass          http://localhost:8082;
		proxy_set_header    X-Real-IP $remote_addr;
		}

    }

要上面提示那两行加上吗?

是的, 加上

                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade; // 这个
                    proxy_set_header Connection $http_connection; // 还有这个

如果没有配置proxy_read_timeout和proxy_send_timeout, 那也得加上

location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_pass http://localhost:8082;
proxy_set_header X-Real-IP $remote_addr;

    }

这么配置后,重启了nginx和tomcat,没效果呀

还需要看看请求是否已经到达NutFilter, 会打印 no action found 之类的就对了

timeout 起码设置1小时.

 proxy_send_timeout 1h;
 proxy_read_timeout 1h;

终极解决方案:
nginx配置

	server {
        listen       80;
        server_name  aa.xx.bb;
        #charset koi8-r;

        access_log  logs/ssu8.cn.access.log  main;
		location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
			root D:/soft/ssu8/webapps/ROOT;
			expires      30d;
		}

		location / {
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection $http_connection;
		proxy_send_timeout 1h;
		proxy_read_timeout 1h;
		proxy_pass          http://localhost:8082;
		proxy_set_header    X-Real-IP $remote_addr;
		
		}

    }

tomcat版本:apache-tomcat-9.0.0.M11
请保证tomcat服务下,lib中有tomcat-websocket.jar和websocket-api.jar,
nginx配置不正常或者没有对应的jar都不能正常的连接websocket

添加回复
请先登陆
回到顶部