[英]WebSocket in the app
I do not speak English well, so sorry. 我的英语说得不好,很抱歉。 Here is my working WebSocket, which works in the browser.
这是我在浏览器中正常工作的WebSocket。 I need to write it to my application (react native).
我需要将其写入我的应用程序(反应本机)。 Please help me write it.
请帮我写。 I can not figure it out for two days already.
我已经两天不知道了。
<html> <head> <meta charset="utf-8"> <title>charset</title> </head> <script> reader = new FileReader(); reader.onload = function() { alert(reader.result); } var socket = new WebSocket("ws://77.87.917.23:8023", 'binary'); socket.onopen = function() { alert("Соединение установлено."); }; socket.onclose = function(event) { if (event.wasClean) { alert('Соединение закрыто чисто'); } else { alert('Обрыв соединения'); } alert('Код: ' + event.code + ' причина: ' + event.reason); }; socket.onmessage = function(event) { reader.readAsText(event.data); }; </script> </html>
You can use it by this way.You can create instance of socket in component constructor or componentDidMount function. 您可以通过这种方式使用它。您可以在组件构造函数或componentDidMount函数中创建套接字的实例。
import io from 'socket.io-client';
const socket = io.connect("http://77.87.917.23:8023",transports: ['websocket']});
socket.on('connect', (socket) => {
console.log('Sono -> connect.');
});
//you can call this function on button click or any other way from the react native component
function sendData(data)
{
socket.emit(event_name,data);// catch this event on server side
}
//This event is fire from server side
socket.on('exchange', function(data){
console.log("share parametere",data);
});
If you have any query then feel free to ask me. 如果您有任何疑问,请随时问我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.