javascript – 与龙卷风websockets传递的消息
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 与龙卷风websockets传递的消息,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2717字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 与龙卷风websockets传递的消息](/upload/InfoBanner/zyjiaocheng/802/c399e5a383874b9a90eea895eb77511e.jpg)
我正在使用龙卷风websockets来更新页面上的一些信息.如果用户在屏幕上改变某些内容,我希望将这些更改显示给具有活动连接的所有其他用户.我可以获取javascript将消息发送到服务器,但我无法弄清楚如何将该消息发送回客户端.这是javascript和python
$(document).ready(function () {
var ws = new WebSocket("company website, I know this works");
ws.onopen = function () {
console.log("websocket engage");
};
ws.onmessage = $(".column li").on("mouseup", function (evt) {
pid = $(this).attr("id");
oldCid = $(this).parent().parent().attr("id");
newCid = $(this).parent().attr("id");
message = pid + " " + oldCid + " " + newCid;
ws.send(message);
});
ws.onclose = function (evt) {
console.log("connection closed");
};
ws.writemessage = function (evt) {
alert(evt.data);
};
});
这是python代码:
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
change = "original"
listeners = []
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print "opened a new websocket"
listeners.append(self)
print listeners
def on_message(self, message):
#self.write_message(u"You Said: " + message)
print ("in on_message " + message)
change = message
#self.write_message(message)
def on_close(self):
print 'connection closed'
listeners.remove(self)
def write_message(self, message):
print ("in write message " + change)
self.write_message(change)
def main():
#tornado.options.parse_command_line()
application = tornado.web.Application([
(r'/ws', WSHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
解决方法:
在python代码中,您已经映射/ ws到WSHandler类,但是在创建WebSocket对象时,您需要在javascript代码中执行此操作:
1)
var ws=new WebSocket("ws://192.168.3.33:8888/ws");
/*
if 192.168.3.33 is the server IP and 8888 is the port, the server is serving on.
So object is mapped to r"/ws". so now the server can identify the request
*/
代替 :
var ws = new WebSocket("company website, I know this works");
2)WebSocket的onmessage()事件发生在服务器发送回来的东西时.
所以javascript代码看起来像这样:
$(document).ready(function () {
var ws=new WebSocket("ws://192.168.3.33:8888/ws");
ws.onopen = function () {
console.log("websocket engage");
};
ws.onmessage = function(evt){
//the received content is in evt.data, set it where you want
};
ws.onclose = function () {
console.log("connection closed");
};
$(".column li").on("mouseup") //the function that sends data
{
pid = $(this).attr("id");
oldCid = $(this).parent().parent().attr("id");
newCid = $(this).parent().attr("id");
message = pid + " " + oldCid + " " + newCid;
ws.send(message);
};
});
内容总结
以上是互联网集市为您收集整理的javascript – 与龙卷风websockets传递的消息全部内容,希望文章能够帮你解决javascript – 与龙卷风websockets传递的消息所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。