我在flutter应用程序中应用了本地Web Socket API,它抛出异常,尝试了很多但找不到任何解决方案,API在Laravel中,在Laravel方面我也使用Socket.io包和Laravel-echo,这里是我的flutter连接代码
void connectToSocket() {
try {
/// You can change your localhost [IPV4] here.
String localhostIp = '192.168.42.48';
String socketUrl = 'http://$localhostIp:8000';
String apiUrl = 'http://$localhostIp:8000/api/users';
String socketPath = apiUrl.replaceAll(socketUrl, '');
socket = IO.io(
socketUrl,
<String, dynamic>{
'transports': ['websocket'],
'autoConnect': false,
'path': socketPath,
'Connection': 'Upgrade',
'Upgrade': 'websocket',
'Sec-WebSocket-Version': '4.6.2',
'Sec-WebSocket-Extensions': 'client_max_window_bits',
},
);
socket!.onConnect((_) {
log('Connected to socket!');
});
socket!.onError((error) {
log('Socket error: $error');
});
socket!.on('newMessage', (data) {
log('New message: $data');
});
socket!.connect();
} catch (e) {
log('Error connecting to socket: $e');
}
}
1条答案
按热度按时间62lalag41#
我认为,socket url需要这样:ws:192.168.42.48:{somePort}/API/users,not http.