简体   繁体   English

如何使用javascript作为客户端和python作为web服务器在websocets之间进行通信

[英]How to communicate between websocets using javascript as client and python as web server

I have connected my web server and client its working. 我已连接我的Web服务器和客户端,其工作正常。 Now I want to call variables that's passing from backend and do a counting 现在我想调用从后端传递过来的变量并进行计数

for example: backend python is passing 2 variables called price of an apple and no.of apples I need to count the price of all apples and print it in client side using javascript 例如:后端python正在传递2个变量,分别为一个苹果的价格和一个苹果的价格。我需要计算所有苹果的价格,并使用javascript在客户端打印出来

Any help is appreciated 任何帮助表示赞赏

In my case I have done with this approach, 就我而言,我已经采用了这种方法,

  • I have created one JS file for WebSocket event handling methods(onOpen,onClose and onMessage) which is loaded on home page loading. 我为WebSocket事件处理方法(onOpen,onClose和onMessage)创建了一个JS文件,该文件在首页加载时加载。
  • In this file I have created one reference variable to hold events of WebSockets using (.prototype) 在此文件中,我创建了一个参考变量,以使用(.prototype)来保存WebSockets的事件。
  • Same like second step created two seperate methods for creating connection which is going to be called from home page and one connect method to connect the server 与第二步一样,创建了两个用于创建连接的独立方法(将从主页调用该方法)和一个用于连接服务器的connect方法
  • In onMessage you can call your business method where you can put your logic 在onMessage中,您可以调用业务方法,并在其中放置逻辑

code look like, 代码看起来像

var _wObj = null;
function WSSocket(tObj) {
    //basic properties
}

create connection 建立连接

WSSocket.createConnection = function ( tObj ) {
    if (!_wObj) {
        _wObj = new WSSocket(tObj);
    }
    return _wObj;
}

connect to server 连接到服务器

WSSocket.prototype.connect = function () {
    //connection logic
}

register events 注册事件

WSSocket.prototype.registerEvents = function () {
        //registering logic
        this.wObj.onmessage = function (event) {
             //call your operation method
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AJAX 和 Flask 在 python 服务器和 javascript 客户端之间进行通信? - How to communicate between python server and javascript client using AJAX and Flask? 服务器端的python脚本与网络浏览器中的javascript之间进行通信 - communicate between python script on server side and javascript in the web-browser 在没有服务器的Javascript中客户端之间进行通信 - Communicate between client in Javascript without server 如何在D服务器和JavaScript之间进行通信 - How to communicate between d server and javascript 在Javascript Web脚本和Erlang服务器之间进行通信的最简单方法是什么 - What is the easiest way to communicate between Javascript web script to an Erlang server 如何在CGI脚本中的JavaScript和Python之间进行通信 - How to communicate between JavaScript and Python in a CGI script 如何使用相同的Restful后端在两个单独的Web应用程序(javascript)之间进行通信? - How to communicate between two separate web apps (javascript) using the same restful backend? 使用Python与JavaScript通信? - Using Python to communicate with JavaScript? 如何使用javascript通过客户端串口进行通信? - how to communicate through serial port on client side using javascript? 使用node.js在服务器和客户端之间进行通信 - communicate between a server and a client with node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM