我很难在我正在开发的客户端上接收集线器消息。我已经能够连接并接收初始初始化消息:
{"C":"d-3D0A11A6-w,E0|7,0|8,1","S":1,"M":[]}
然而,当下一条消息到达时,即从服务器中心收到消息时,我会遇到意外行为。我收到以下内容:
&{"C":"d-3D0A11A6-:,1|BA,0|BB,1","M":[
但我没有收到实际的消息。我认为消息的有效载荷类似于以下内容:
{"H":"hub", "M":"broadcast", "A":["Hi!", 1]}
我尝试一次读取1个字符的数据,也尝试读取1024个更大的块。但似乎没有任何东西从套接字中读取消息。
以下是我在接收到初始消息后所做的读取示例:
int recv_to(int fd, char *buffer, int len, int flags, int to) {
fd_set readset;
int result, iof = -1;
struct timeval tv;
// Initialize the set
FD_ZERO(&readset);
FD_SET(fd, &readset);
// Initialize time out struct
tv.tv_sec = 3;
tv.tv_usec = to * 1000;
// select()
result = select(fd+1, &readset, NULL, NULL, &tv);
// Check status
if (result < 0)
return -1;
else if (result > 0 && FD_ISSET(fd, &readset)) {
// Set non-blocking mode
if ((iof = fcntl(fd, F_GETFL, 0)) != -1)
fcntl(fd, F_SETFL, iof | O_NONBLOCK);
// receive
result = recv(fd, buffer, len, flags);
// set as before
if (iof != -1)
fcntl(fd, F_SETFL, iof);
return result;
}
return -2;
}
int wait = 2;
do {
if((n = recv_to(sockfd, buffer, 1024, 0, wait)) > -1) {
printMsg(buffer);
bzero(buffer, 1024);
}
else if(n != -1) {
printf("received: %d\n", n);
}
else {
printf("%s", "error");
break;
}
} while(1);
我已经确认我使用升级后的连接来使用websockets。这是谈判网址:
blahblah.azurewebsites.net/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B"name":"broadcasthub"%7D%5D