我从Web套接字开始,对在angular中使用它感到困惑。
下面是使用socket.io发送数据的后端代码
./gradlew run
如何连接到此并接收数据。我尝试按照一些教程进行操作,但不确定我是否正确,在控制台中出现错误,当我尝试获取数据时出现错误
var milo_socket_io = io
.of('/milo')
.on('connection', function(socket)
{
console.log("Client connected " + socket.id);
clients.push(socket);
});
function sendLiveData(jsonStr)
{
switch (jsonStr.deviceType)
{
case 'milo' :
milo_socket_io.emit('data', JSON.stringify(jsonStr));
break;
default:
console.log("Should not happen. Device Type " + jsonStr.deviceType)
}
}
这是我在客户端尝试过的
websocket.js:118 WebSocket connection to 'ws://.../socket.io/?EIO=3&transport=websocket&sid=AqFUpGtwfSsoDXvGAAIc' failed: Error during WebSocket handshake: Unexpected response code: 400
In service.ts
export class WebSocketService {
socket: SocketIOClient.Socket;
constructor() {
}
observer : Observer<number> ;
recieveDate(){
this.socket = socketio('http://ip:port/milo')
this.socket.on('emit',(res) => {
this.observer.next(res)
});
return this.createObservable()
}
createObservable() : Observable<number> {
return new Observable(observer => {
this.observer = observer;
});
}
}
答案 0 :(得分:0)