Replies: 9 comments 8 replies
-
Are you using those crates inside your template code? If so, that's wrong, you need to use The problem is, if I'm right, that you're using a server-side crate on the client-side, a mistake that will be made much harder to make in the next version (I'll make these sorts of distinctions far clearer). As for your note on TCP, what do you mean there? TCP is the basis of HTTP, which is pretty much the universal standard for connecting to servers supplying static information to a Web browser. To my understanding, browsers only provide support for certain types of requests, you can't use TCP directly, only through HTTP. (I may be wrong on that though...) |
Beta Was this translation helpful? Give feedback.
-
Thanks, I think the point is clearer. I thought anything pertaining the |
Beta Was this translation helpful? Give feedback.
-
per update on the discord channel, here is the code while attempting to use [package]
name = "front"
version = "0.0.1"
edition = "2021"
[dependencies]
perseus = { version = "0.3.5", features = [ "hydrate" ] }
sycamore = "0.7"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
# ureq = "2"
reqwasm = "0.4"
# reqwest = "0.11.10"
reqwest = { version = "0.11.10", features = ["json"] }
# hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] }
pretty_env_logger = "0.4"
// use hyper::Client;
use perseus::{Html, RenderFnResultWithCause, Template};
use sycamore::prelude::*;
#[perseus::make_rx(IndexPageStateRx)]
pub struct IndexPageState {
server_ip: String,
browser_ip: Option<String>,
}
#[perseus::template_rx]
pub fn index_page(
IndexPageStateRx {
server_ip,
browser_ip,
}: IndexPageStateRx,
) -> View<G> {
// This will only run in the browser
// `reqwasm` wraps browser-specific APIs, so we don't want it running on the server
// If the browser IP has already been fetched (e.g. if we've come here for the second time in the same session), we won't bother re-fetching
if G::IS_BROWSER && browser_ip.get().is_none() {
// Spawn a `Future` on this thread to fetch the data (`spawn_local` is re-exported from `wasm-bindgen-futures`)
// Don't worry, this doesn't need to be sent to JavaScript for execution
//
// We want to access the `message` `Signal`, so we'll clone it in (and then we need `move` because this has to be `'static`)
perseus::spawn_local(cloned!(browser_ip => async move {
// This interface may seem weird, that's because it wraps the browser's Fetch API
// We request from a local path here because of CORS restrictions (see the book)
let body = reqwasm::http::Request::get("/.perseus/static/message.txt")
.send()
.await
.unwrap()
.text()
.await
.unwrap();
browser_ip.set(Some(body));
}));
}
// If the future hasn't finished yet, we'll display a placeholder
// We use the wacky `&*` syntax to get the content of the `browser_ip` `Signal` and then we tell Rust to take a reference to that (we can't move it out because it might be used later)
let browser_ip_display = match &*browser_ip.get() {
Some(ip) => ip.to_string(),
None => "fetching".to_string(),
};
view! {
p { (format!("IP address of the server was: {}", server_ip.get())) }
p { (format!("The message is: {}", browser_ip_display)) }
}
}
pub fn get_template<G: Html>() -> Template<G> {
Template::new("index")
.build_state_fn(get_build_state)
.template(index_page)
}
#[perseus::autoserde(build_state)]
pub async fn get_build_state(
_path: String,
_locale: String,
) -> RenderFnResultWithCause<IndexPageState> {
// We'll cache the result with `try_cache_res`, which means we only make the request once, and future builds will use the cached result (speeds up development)
// let body = perseus::cache_fallible_res(
// "ipify",
// || async {
// // This just gets the IP address of the machine that built the app
// let res = ureq::get("https://api.ipify.org").call()?.into_string()?;
// Ok::<String, ureq::Error>(res)
// },
// false,
// )
// .await?;
// Ok(IndexPageState {
// server_ip: body,
// browser_ip: None,
// })
let res = reqwest::get("http://httpbin.org/get").await?;
println!("Status: {}", res.status());
println!("Headers:\n{:#?}", res.headers());
let body = res.text().await?;
println!("Body:\n{}", body);
Ok(IndexPageState {
server_ip: body,
browser_ip: None,
})
} The Error Log. ##The error so far.
[INFO]: Compiling to Wasm... error: Socket2 doesn't support the compile target error[E0432]: unresolved imports error[E0432]: unresolved imports error[E0432]: unresolved import error[E0433]: failed to resolve: could not find error[E0425]: cannot find function error[E0425]: cannot find function error[E0425]: cannot find function error[E0425]: cannot find function error[E0412]: cannot find type error[E0425]: cannot find function 109 | pub(crate) fn as_raw(&self) -> sys::Socket { error[E0425]: cannot find function --> /home/afidegnum/.cargo/registry/src/github.com-1ecc6299db9ec823/socket2-0.4.4/src/socket.rs:113:42
114 | sys::socket_into_raw(self.inner) error[E0425]: cannot find function
181 | sys::bind(self.as_raw(), address) error[E0425]: cannot find function error[E0425]: cannot find function error[E0425]: cannot find function error[E0425]: cannot find function
309 | sys::getsockname(self.as_raw()) error[E0425]: cannot find function
326 | unsafe { getsockopt::<c_int>(self.as_raw(), sys::SOL_SOCKET, sys::SO_TYPE).map(Type) } error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌
343 | sys::try_clone(self.as_raw()).map(Socket::from_raw)
355 | sys::set_nonblocking(self.as_raw(), nonblocking) error[E0425]: cannot find function error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌
help: consider importing this type alias error[E0425]: cannot find function [2/4] 🏗️ Building your app to Wasm...❌ [2/4] 🏗️ Building your app to Wasm...❌ 482 | self.recv_with_flags(buf, sys::MSG_PEEK) error[E0425]: cannot find function error[E0425]: cannot find function [2/4] 🏗️ Building your app to Wasm...❌ error[E0425]: cannot find function error[E0425]: cannot find function 617 | self.send_with_flags(buf, sys::MSG_OOB) error[E0425]: cannot find function 661 | sys::send_to_vectored(self.as_raw(), bufs, addr, flags) 748 | getsockopt::<c_int>(self.as_raw(), sys::SOL_SOCKET, sys::SO_BROADCAST) error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌ 774 | match unsafe { getsockopt::<c_int>(self.as_raw(), sys::SOL_SOCKET, sys::SO_ERROR) } { 774 | match unsafe { getsockopt::<c_int>(self.as_raw(), sys::SOL_SOCKET, sys::SO_ERROR) } { error[E0425]: cannot find value 788 | getsockopt::(self.as_raw(), sys::SOL_SOCKET, sys::SO_KEEPALIVE) error[E0425]: cannot find value error[E0425]: cannot find value 814 | getsockopt::sys::linger(self.as_raw(), sys::SOL_SOCKET, sys::SO_LINGER) error[E0425]: cannot find value error[E0425]: cannot find value 835 | unsafe { setsockopt(self.as_raw(), sys::SOL_SOCKET, sys::SO_LINGER, linger) } error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌ error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value 892 | sys::SO_RCVBUF, error[E0425]: cannot find function error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find function [2/4] 🏗️ Building your app to Wasm...❌ET 911 | sys::set_timeout_opt(self.as_raw(), sys::SOL_SOCKET, sys::SO_RCVTIMEO, duration) error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌ [2/4] 🏗️ Building your app to Wasm...❌ error[E0425]: cannot find value error[E0425]: cannot find function error[E0425]: cannot find value error[E0425]: cannot find value 982 | sys::set_timeout_opt(self.as_raw(), sys::SOL_SOCKET, sys::SO_SNDTIMEO, duration) 982 | sys::set_timeout_opt(self.as_raw(), sys::SOL_SOCKET, sys::SO_SNDTIMEO, duration) error[E0425]: cannot find value error[E0412]: cannot find type error[E0412]: cannot find type 996 | Some(duration) => sys::linger { error[E0422]: cannot find struct, variant or union type error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌ 1045 | sys::IP_HDRINCL, 1101 | let mreq = sys::IpMreq { error[E0425]: cannot find function error[E0425]: cannot find function [2/4] 🏗️ Building your app to Wasm...❌ error[E0425]: cannot find value 1114 | let mreq = sys::IpMreq { error[E0425]: cannot find function error[E0425]: cannot find function [2/4] 🏗️ Building your app to Wasm...❌IP 1121 | sys::IPPROTO_IP, error[E0425]: cannot find value error[E0425]: cannot find function [2/4] 🏗️ Building your app to Wasm...❌ error[E0425]: cannot find value 1174 | let mreqn = sys::to_mreqn(multiaddr, interface); error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value 1192 | getsockopt(self.as_raw(), sys::IPPROTO_IP, sys::IP_MULTICAST_IF).map(sys::from_in_addr) error[E0425]: cannot find value 1200 | let interface = sys::to_in_addr(interface); 1204 | sys::IPPROTO_IP, error[E0425]: cannot find value 1218 | getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, sys::IP_MULTICAST_LOOP) error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌in module 1284 | unsafe { setsockopt(self.as_raw(), sys::IPPROTO_IP, sys::IP_TTL, ttl as c_int) } error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌lf.as_raw(), sys::IPPROTO_IP, sys::IP_TOS).map(|tos| tos as u32) error[E0422]: cannot find struct, variant or union type error[E0425]: cannot find function error[E0425]: cannot find value error[E0425]: cannot find value error[E0422]: cannot find struct, variant or union type error[E0425]: cannot find function 1370 | sys::IPPROTO_IPV6, error[E0425]: cannot find value 1384 | getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_MULTICAST_HOPS) 1384 | getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_MULTICAST_HOPS) error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌TICAST_IF 1412 | getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_MULTICAST_IF) error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value 1440 | getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_MULTICAST_LOOP) error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌CAST_HOPS 1465 | getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_UNICAST_HOPS) error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌lf.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_V6ONLY) error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌ [2/4] 🏗️ Building your app to Wasm...❌cp_keepalive 1668 | sys::set_tcp_keepalive(self.as_raw(), params) error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value 1695 | sys::TCP_NODELAY, error[E0425]: cannot find value 161 | pub const IPV6: Domain = Domain(sys::AF_INET6); error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌ 205 | pub const DGRAM: Type = Type(sys::SOCK_DGRAM); error[E0425]: cannot find value error[E0425]: cannot find value error[E0425]: cannot find value [2/4] 🏗️ Building your app to Wasm...❌ [2/4] 🏗️ Building your app to Wasm...❌TCP 248 | pub const TCP: Protocol = Protocol(sys::IPPROTO_TCP); error[E0425]: cannot find value error[E0425]: cannot find value error[E0412]: cannot find type [2/4] 🏗️ Building your app to Wasm...❌ Compiling serde_derive v1.0.137 129 | init(storage.as_mut_ptr(), &mut len).map(|res| { error[E0061]: this function takes 4 arguments but 3 arguments were supplied [2/4] 🏗️ Building your app to Wasm...❌ [2/4] 🏗️ Building your app to Wasm...❌ 380 | pub fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 { error[E0308]: mismatched types
104 | fn from(socket: $from) -> $for { ⠂ [1/4] 🔨 Generating your app...
104 | fn from(socket: $from) -> $for {
1778 | from!(net::UdpSocket, Socket); error[E0308]: mismatched types [2/4] 🏗️ Building your app to Wasm...❌rc/github.com-1ecc6299db9ec823/socket2-0.4.4/src/socket.rs:1780:15
104 | fn from(socket: $from) -> $for {
1781 | from!(Socket, net::UdpSocket); 104 | fn from(socket: $from) -> $for { Some errors have detailed explanations: E0061, E0308, E0412, E0422, E0425, E0432, E0433, E0583, E0599. [2/4] 🏗️ Building your app to Wasm...❌ |
Beta Was this translation helpful? Give feedback.
-
That looks like it shouldn't happen, I'll investigate further and get back to you. |
Beta Was this translation helpful? Give feedback.
-
after reading from a previous issue I tried modifying my code which also didn't help
use std::fmt::Display;
// use hyper::Client;
use perseus::{Html, RenderFnResultWithCause, Template};
use serde::{Deserialize, Serialize};
use sycamore::prelude::*;
#[perseus::make_rx(IndexPageStateRx)]
pub struct IndexPageState {
server_ip: String,
browser_ip: Option<String>,
m_text: MarkdownPageProps,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MarkdownPageProps {
pub text: String,
}
#[cfg(not(target_arch = "wasm32"))]
pub async fn get_ip() -> Result<MarkdownPageProps, reqwest::Error> {
// let content = surf::get("https://api.ipify.org?format=json").recv_json();
// content.await;
let res = reqwest::get("http://httpbin.org/get").await.unwrap();
println!("Status: {}", res.status());
println!("Headers:\n{:#?}", res.headers());
let body = res.text().await.unwrap();
println!("Body:\n{}", body);
Ok(MarkdownPageProps { text: body })
}
#[perseus::template_rx]
pub fn index_page(
IndexPageStateRx {
server_ip,
browser_ip,
m_text,
}: IndexPageStateRx,
) -> View<G> {
// This will only run in the browser
// `reqwasm` wraps browser-specific APIs, so we don't want it running on the server
// If the browser IP has already been fetched (e.g. if we've come here for the second time in the same session), we won't bother re-fetching
if G::IS_BROWSER && browser_ip.get().is_none() {
// Spawn a `Future` on this thread to fetch the data (`spawn_local` is re-exported from `wasm-bindgen-futures`)
// Don't worry, this doesn't need to be sent to JavaScript for execution
//
// We want to access the `message` `Signal`, so we'll clone it in (and then we need `move` because this has to be `'static`)
perseus::spawn_local(cloned!(browser_ip => async move {
// This interface may seem weird, that's because it wraps the browser's Fetch API
// We request from a local path here because of CORS restrictions (see the book)
let body = reqwasm::http::Request::get("/.perseus/static/message.txt")
.send()
.await
.unwrap()
.text()
.await
.unwrap();
browser_ip.set(Some(body));
}));
}
// If the future hasn't finished yet, we'll display a placeholder
// We use the wacky `&*` syntax to get the content of the `browser_ip` `Signal` and then we tell Rust to take a reference to that (we can't move it out because it might be used later)
let browser_ip_display = match &*browser_ip.get() {
Some(ip) => ip.to_string(),
None => "fetching".to_string(),
};
view! {
p { (format!("IP address of the server was: {}", server_ip.get())) }
p { (format!("The message is: {}", browser_ip_display)) }
p { (format!("The message is: {:?}", m_text.get())) }
}
}
pub fn get_template<G: Html>() -> Template<G> {
Template::new("index")
.build_state_fn(get_build_state)
.template(index_page)
}
#[perseus::autoserde(build_state)]
pub async fn get_build_state(
_path: String,
_locale: String,
) -> RenderFnResultWithCause<IndexPageState> {
// We'll cache the result with `try_cache_res`, which means we only make the request once, and future builds will use the cached result (speeds up development)
// let body = perseus::cache_fallible_res(
// "ipify",
// || async {
// // This just gets the IP address of the machine that built the app
// let res = ureq::get("https://api.ipify.org").call()?.into_string()?;
// Ok::<String, ureq::Error>(res)
// },
// false,
// )
// .await?;
// Ok(IndexPageState {
// server_ip: body,
// browser_ip: None,
// })
// let res = reqwest::get("http://httpbin.org/get").await?;
// println!("Status: {}", res.status());
// println!("Headers:\n{:#?}", res.headers());
// let body = res.text().await?;
// println!("Body:\n{}", body);
Ok(IndexPageState {
server_ip: "None".to_string(),
browser_ip: None,
m_text: get_ip().await.unwrap(),
})
} |
Beta Was this translation helpful? Give feedback.
-
From what I can see so far, reqwasm , hyper, including surf, accept parallel requests meanwhile ureq stick to blocking I/O requests and using agents to handle concurrent requests. in some case, we need some parallel request to load data from numerous sources, be it on the same server or other sources. i.e product catalog, rss feeds. |
Beta Was this translation helpful? Give feedback.
-
As I mentioned before, |
Beta Was this translation helpful? Give feedback.
-
ok, Thanks |
Beta Was this translation helpful? Give feedback.
-
Hold on, I see that you already have a section in your |
Beta Was this translation helpful? Give feedback.
-
While trying to use reqwest or hyper to be able to connect to my side API server, I faced the above error.
Do you have any idea on mitigating this issue?
I noticed a similar issue is being reported at rust-lang/socket2#35 and rust-lang/socket2#28 (comment)
What alternative do you suggest ?
I'm also wondering, won't it be best to exchange Requests via Unix Domain Socket instead of TCP, this will be more performant and less burden?
.
Beta Was this translation helpful? Give feedback.
All reactions