如何在客户端关闭套接字连接?
我在用:
即:呼叫localhost/test
- 服务器端
var test = io
.of('/test')
.on('connection', function (socket) {
console.log('open socket: ' + socket);
socket.on('disconnect', function () {
console.log('disconnected event');
//socket.manager.onClientDisconnect(socket.id); --> endless loop with this disconnect event on server side
//socket.disconnect(); --> same here
});
});
Run Code Online (Sandbox Code Playgroud)
- 客户端
var socket = io.connect('http://localhost:3000/test');
socket.on('disconnect', function () {
console.log('disconnect client event....');
});
socket.emit('getInitData', function (data) {
.. do something with data
});
Run Code Online (Sandbox Code Playgroud)
如果我加载测试页面,我需要来自服务器的一些值(getInitData).
在第一页访问我获取数据一次,在重新加载或第二次访问我得到它两次,依此类推.
服务器端的连接在页面重新加载时自动关闭,如果您离开页面.
但在客户端,连接仍然是开放的.
如何在客户端关闭连接或检查是否已打开连接?
更新
我现在尝试以下内容:(客户端)
window.onbeforeunload …Run Code Online (Sandbox Code Playgroud) 有人可以解释各类型之间的差异uint8_t和__u8?
我知道这uint8_t是在stdint.h中定义的,并且它们在每个unix系统上都可用.
/* Unsigned. */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
...
Run Code Online (Sandbox Code Playgroud)
如果我使用它们可以识别我打算做什么.
现在我偶然发现了__u8和__u16类型.它似乎对我来说是一样的.
其中一些类型在linux/types.h中定义
#ifdef __CHECKER__
#define __bitwise__ __attribute__((bitwise))
#else
#define __bitwise__
#endif
#ifdef __CHECK_ENDIAN__
#define __bitwise __bitwise__
#else
#define __bitwise
#endif
typedef __u16 __bitwise __le16;
typedef __u16 __bitwise __be16;
typedef __u32 __bitwise __le32;
...
Run Code Online (Sandbox Code Playgroud)
我没找到,__u8但我仍然可以使用它,它的行为就像uint8_t.
性能或内存消耗有一些差异吗?
感谢帮助 :)
可有人请澄清差的包括选项
#include <atomic>和#inlucde <cstdatomic>?
我猜是没有,因为它的行为相同?
我问这个是因为在我的debian系统中我只有原子和我的kubuntu系统我得到了cstdatomic.
Debian上的编译器:版本4.7.2(Debian 4.7.2-4)
Kubuntu上的编译器:版本4.6.3(Ubuntu/Linaro 4.6.3-1ubuntu5)
我正在使用带有类的c ++ 11线程库,它工作正常.
我只需要对此代码进行解释,以便我理解正确.
我的class.h
class foo {
private:
std::thread t1;
void worker();
public:
void work();
};
Run Code Online (Sandbox Code Playgroud)
class.cpp
#include "class.h"
void foo::worker() {
std::cout << "worker..." << std::endl;
}
void foo::work() {
t1 = std::thread(&foo::worker, this);
t1.join();
}
Run Code Online (Sandbox Code Playgroud)
现在是main.cpp
#include "class.h"
int main(int argc, char **argv) {
foo bar;
bar.work();
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白的是调用线程的类函数.
我使用std::thread(&foo::work, this)和解释这个调用如下:第一个参数是指向函数的指针,但我不知道为什么我不能在没有该&foo::部分的情况下调用它.第二个参数是线程知道父进程的类本身?
我找不到这方面的解释.只有代码,我想了解它.谢谢!
我正在编写一个连接到CAN-Interface的c ++程序(用于arm-architecture).我正在使用标准套接字,绑定,recv和发送功能.现在,我需要将一些功能外包给线程.为此,我想使用C++ 0x Threads,因为我在这里读到了pthreads不应该在c ++中使用的兼容性问题.
所以我包括线程库#include <thread>.并添加到我的编译器调用选项-Wno-psabi -std=c++0x -lpthread
(-Wno-psabi是否禁用该note: the mangling of ‘va_list’ has changed in GCC 4.4消息)
我得到的错误是:
25: error: no match for ‘operator<’ in ‘std::bind(_Functor, _ArgTypes ...) [with _Functor = int, _ArgTypes = sockaddr*, unsigned int](((sockaddr*)(&((can*)this)->can::addr)), 16u) < 0
/usr/arm-linux-gnueabi/include/c++/4.4.5/system_error:258: note: candidates are: bool std::operator<(const std::error_condition&, const std::error_condition&)
/usr/arm-linux-gnueabi/include/c++/4.4.5/system_error:177: note: bool std::operator<(const std::error_code&, const std::error_code&)
我认为来自线程库的bind函数正在从套接字覆盖bind函数.
我如何告诉编译器何时使用什么函数?
即时通讯使用arm-linux-gnueabi-g++4.4.5版
我需要按需创建一些线程,因为我不知道我需要多少.std::vector<std::thread> threads;
每次我创建一个新线程时,使用向量"存储" 线程我将它们推回到向量上:threads.push_back(std::thread(worker));
存储(用于测试)线程id我使用以下内容:double test = std::hash<std::thread::id>()(std::this_thread::get_id());
根据cpp-references hash,thread :: id和get_id,这应该可以正常工作.
但只有这条线我得到的ID总是0(零)
如果id在哈希行下面添加以下行,它会工作,我从线程中得到一个哈希的id:std::thread::id tid = std::this_thread::get_id();
即使我不使用tid
有人可以解释这种行为吗?我正在使用eclipse juno并清理+重建项目几次...我只是不明白:/
这里的代码:(逐行,因为这里的格式规则是愚蠢的 - .-
std::vector<std::thread> threads;
void worker (){
double test = std::hash<std::thread::id>()(std::this_thread::get_id());
std::thread::id tid = std::this_thread::get_id();
printf("%ld\n", test);
}
void joinThreads(std::thread& t)
{
t.join();
}
int main() {
for (int i = 0; i < 10; ++i) {
threads.push_back(std::thread(worker) );
}
std::for_each(threads.begin(),threads.end(),joinThreads);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个uint16_t变量,我必须设置特定的位.
例:
uint16_t field = 0;
这意味着这些位都是零: 0000 0000 0000 0000
现在我得到了一些我需要在特定位置设置的值.
val1=1; val2=2, val3=0, val4=4, val5=0;
如何设置位的结构如下
0|000| 0000| 0000 000|0
Run Code Online (Sandbox Code Playgroud)
val1应该设置在左边的第一位.所以它只有一个或零.
val2应该设置在接下来的三位.val3在接下来的四位上.val4在接下来的七位和val5一位最后一位.
结果是这样的:
1010 0000 0000 1000
我只发现了一个特定的位而不是"组".(移位或bitset)
有谁知道如何解决这个问题?
我将第二个,分钟,小时,日,月和年作为我的应用程序中的单个值,并且需要从此值生成一个unix时间戳.
我只找到了如何直接获得C函数的时间戳的解决方案.
如何使用C++中的给定值生成unix时间戳?