yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications
https://yew.rs
Apache License 2.0
30.77k stars 1.43k forks source link

browser display error on tcp programming in yew #3173

Closed mikewang68 closed 1 year ago

mikewang68 commented 1 year ago

Problem

use std::io::{prelude::*, BufReader, Write}; use std::net::TcpStream; use std::str;

pub fn socket_client(s: &str) -> String { let mut stream = TcpStream::connect("127.0.0.1:10000").unwrap();

let input = s;
stream
    .write(input.as_bytes())
    .expect("Failed to write to stream");
let mut reader = BufReader::new(&stream);
let mut buffer: Vec<u8> = Vec::new();
reader
    .read_until(b'\n', &mut buffer)
    .expect("Could not read into buffer");
let ss = str::from_utf8(&buffer).expect("Could not write buffer as string");
let ss = ss.to_owned();
ss

}


error yew-app-f2961ee15f0adfac.js:320 panicked at 'called Result::unwrap() on an Err value: Error { kind: Unsupported, message: "operation not supported on this platform" }', src/socket.rs:6:60

Stack:

Error at imports.wbg.wbg_new_abda76e883ba8a5f (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac.js:326:21) at console_error_panic_hook::Error::new::hcc273312b83619b5 (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac_bg.wasm:wasm-function[3676]:0x1002a6) at console_error_panic_hook::hook_impl::h6e828b44db16a989 (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac_bg.wasm:wasm-function[639]:0x88b8e) at console_error_panic_hook::hook::h27b20f480d424fbb (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac_bg.wasm:wasm-function[4092]:0x105ce2) at core::ops::function::Fn::call::hf927bad82efada78 (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac_bg.wasm:wasm-function[3510]:0xfd97a) at std::panicking::rust_panic_with_hook::hbafe3e603d331223 (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac_bg.wasm:wasm-function[1450]:0xc1762) at std::panicking::begin_panic_handler::{{closure}}::h8ab6ee68d5b4c391 (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac_bg.wasm:wasm-function[1881]:0xd54bc) at std::sys_common::backtrace::rust_end_short_backtrace::h008f69666d134159 (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac_bg.wasm:wasm-function[4282]:0x1082f8) at rust_begin_unwind (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac_bg.wasm:wasm-function[2949]:0xf34e1) at core::panicking::panic_fmt::h1d17fc068f528130 (http://127.0.0.1:8080/yew-app-f2961ee15f0adfac_bg.wasm:wasm-function[3919]:0x103979)

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Environment:

Questionnaire

how to solve it , if use tcp socket programming in yew ? thanks

HeroesLament commented 1 year ago

The Yew crew could chime in with a more specific answer, but because the WASM binary runs within the browser's sandbox and DOM, it can't interact with the kernel to open up arbitrary TCP sockets. However, you could do something like WebSocket or any of the other Fetch APIs exposed to regular web applications.