[英]Unable to establish connection between Node.JS and React-Native (Socket.IO)
I'm new to React
and Node
and i'm trying to make a simple WebSocket
using Socket.IO which gonna simply send greetings to all connected users and the user will respond to the server.我是
React
和Node
的新手,我正在尝试使用Socket.IO制作一个简单的WebSocket
,它会简单地向所有连接的用户发送问候,用户将响应服务器。
The Node.JS
server is running on a Windows PC while the React-Native app is running on both iOS and Android devices. Node.JS
服务器在 Windows PC 上运行,而 React-Native 应用程序在 iOS 和 ZE84E30B9390CDB46ABZDB6DB8 设备上运行。
Node.JS
server code is the following Node.JS
服务器代码如下
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
const bodyParser = require('body-parser');
const mysql = require('mysql');
const connection = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'block',
database : 'visualpos'
});
// Creating a GET route that returns data from the 'users' table.
app.get('/prenotazioni', function (req, res) {
// Connecting to the database.
connection.getConnection(function (err, connection) {
// Executing the MySQL query (select all data from the 'users' table).
connection.query("SELECT Data, Importo_Doc FROM tabella_pagamenti", function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results)
});
connection.release();
});
});
app.get('/', function(req, res){
res.send('<h1>Hello World</h1>');
});
// Starting our server.
http.listen(3000, () => {
console.log('In ascolto sulla porta *:3000');
});
io.emit('saluta', 'Ciao dal server :)');
io.on('connected', (data) => {
console.log(data);
});
Actually GET part of the code works perfectly but the Socket.IO seems death .实际上 GET 部分代码运行良好,但 Socket.IO 似乎已经死亡。 The client doesn't get any response and server the same i think the Socket.IO server simply doesn't start..
客户端没有得到任何响应和服务器相同我认为 Socket.IO 服务器根本没有启动..
In XCode Debug i get the following errors when the app is running on the iPhone在 XCode 调试中,当应用程序在 iPhone 上运行时出现以下错误
And i even get on both devices warning "Unrecognized WebSocket connection option(s) 'agent', 'perMessageDeflate',..."我什至在两台设备上都发出警告“无法识别 WebSocket 连接选项‘代理’,‘perMessageDeflate’,...”
And here is the code i'm using in React-Native这是我在 React-Native 中使用的代码
import io from 'socket.io-client'
var socket = io('http://192.168.100.50:3000', {
jsonp: false,
transports: ['websocket'],
autoConnect: true,
reconnection: true,
reconnectionDelay: 500,
reconnectionAttempts: Infinity
});
componentDidMount(){
socket.emit('connected','we');
socket.on('saluta',(data) => { alert(data); });
}
On socket.io getStarted section, they use a "connection" event instead of "connected" ( https://socket.io/get-started/chat/ ).在 socket.io getStarted 部分,他们使用“连接”事件而不是“连接”( https://socket.io/get-started/chat/ )。
io.on('connection', function(socket){ console.log('a user connected'); socket.on('disconnect', function(){ console.log('user disconnected'); }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.