网页(使用php),即Javascript Websocket客户端未连接到Python Websocket服务器

时间:2018-12-25 15:53:12

标签: javascript python websocket server client

显示如下: WebSocket与'ws://10.14.79.58:1030 / websocket'的连接失败:WebSocket握手时出错:意外的响应代码:404

这是我的客户端示例python服务器代码:

import tornado.ioloop
import tornado.web
import tornado.websocket
from tornado.options import define, options, parse_command_line

#define(1020, default=8888, type=int)


class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("test.html")


class WebSocketHandler(tornado.websocket.WebSocketHandler):
    def open(self, *args):
        print ("New connection")
        self.write_message("Welcome!")

    def on_message(self, message):
        print ("New message {}".format(message))
        self.write_message(message.upper())

    def on_close(self):
        print ("Connection closed")


app = tornado.web.Application([
    (r'/webscoket/', WebSocketHandler),
])


if __name__ == '__main__':
    app.listen(1030)
    tornado.ioloop.IOLoop.instance().start()

这是我的php文件,其中js用于连接:

<!DOCTYPE html>
<head>

</head>
<body>
<h1>This is connect file</h1>
<script>
var connection = new WebSocket('ws://10.14.79.58:1030/websocket');


        connection.onmessage = function (e) {
        //document.getElementById("text").innerHTML = e.data;
        alert("Connection to the websocket established!!");
}

</script>
</body>
</html>

0 个答案:

没有答案