以下示例来自flutter.dev,我正在使用web_socket_channel插件与WebSocket连接 https://flutter.dev/docs/cookbook/networking/web-sockets
但是找不到订阅频道的选项。我有点扑朔迷离。我正在添加我的等效浏览器javascript代码以更好地理解。我想在颤抖中达到同样的效果。
等效的javascript代码->
function startChat() {
ws = adonis.Ws("ws://127.0.0.1:3333").connect();
ws.on("open", () => {
$(".connection-status").addClass("connected");
subscribeToChannel();
});
ws.on("error", () => {
$(".connection-status").removeClass("connected");
});
}
function subscribeToChannel() {
const chat = ws.subscribe("chat");
chat.on("error", () => {
$(".connection-status").removeClass("connected");
});
chat.on("message", message => {
// console.log(message)
$(".messages").append(`
<div class="message"><h3> ${message.username}</h3><p> ${message.body} </p></div>
`);
});
}
$("#message").keyup(function(e) {
if (e.which === 13) {
e.preventDefault();
const message = $(this).getValAndClear();
console.log(message);
ws.getSubscription("chat").emit("message", {
username: window.username,
body: message
});
}
});