尝试为连接到我的web应用程序的每个访问者生成一个UUID。
在开发中,由于热重新加载,同一浏览器中的多个选项卡将获得不同的UUID,因为所有选项卡都会一起刷新。
因为fetch需要一段时间,所以在一秒钟的拆分过程中,localStorage会被生成的不同UUID覆盖。而且我的套接字也被设置为3-4个不同的UUID。
在生产开发中,因为没有热重新加载,这种情况可能很少,但是有什么最佳实践来避免这种情况吗?
componentWillMount() {
if (localStorage.getItem('uuid') === null) {
fetch('http://example.com/visitor/', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
})
.then(response => response.json())
.then(jsondata => {
localStorage.setItem('uuid', jsondata);
this.setState({ socket: openSocket('http://localhost:3005') });
this.state.socket('setname', this.state.userName);
})
.catch(err => console.log(err));
}
}发布于 2018-04-12 14:39:16
那么在客户端生成uuid并将其推送到后台又如何呢?您可以使用像uuid这样的库来实现此目的
https://stackoverflow.com/questions/49789440
复制相似问题