我试图通过下面的python代码控制Font Size属性。我已经通过Websocket发送了数据,Python和JS似乎工作得很好。下面是我的代码尝试。我做错了什么?
提前感谢!:)
function WebSocketTest() {
if ("WebSocket" in window) {
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
var ws = new WebSocket("ws://localhost:8768/echo");
ws.onopen = function() {
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
document.querySelector("h1").style.fontSize = evt.data;
};
ws.onclose = function() {
// websocket is closed.
};
} else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}/*! Generated by Font Squirrel (https://www.fontsquirrel.com) on June 3, 2020 */
@font-face {
font-family: 'spartanthin';
src: url('fonts/SpartanUnconv/Spartan-VariableFont_wght.ttf');
font-weight: 100 900;
font-style: normal;
}
h1 {
margin-bottom: 10px;
font-family: 'spartanthin';
font-weight: var(--font-weight);
font-size: 15px;
text-align: left;
position: relative;
}<html>
<body>
<h1>Heading</h1>
<p>A Black Fox Jumped Over A Fence</p>
</body>
</html>
Python:
import asyncio
import websockets
async def echo(websocket, path):
async for message in websocket:
await websocket.send(str(900)) #FontSize Value
start_server = websockets.serve(echo, "localhost", 8778)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()发布于 2020-06-15 06:58:02
async for message in websocket:意味着您的服务器只响应来自客户端的消息,而您的客户端不发送任何消息。
像这样试一下
import asyncio
import websockets
async def echo(websocket, path):
await websocket.send(str(900)) #FontSize Value
start_server = websockets.serve(echo, "localhost", 8778)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()另外,将JS部件WebSocket("ws://localhost:8768/echo");更改为WebSocket("ws://localhost:8778"); (以便它与您的服务器相对应)。我不确定你是不是在调用那个js函数,所以如果你没有WebSocketTest();,把它放在WebSocketTest中的脚本标签里,或者直接放到那个js文件中的那个js函数定义里。
https://stackoverflow.com/questions/62366934
复制相似问题