栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

确认socket.io自定义事件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

确认socket.io自定义事件

Emmit方法的第三个参数接受一个回调,该回调将传递给服务器,以便您可以调用所需的任何数据进行确认。实际上,这确实非常方便,并且节省了配对呼叫响应事件的工作量。

我正在使用刚刚测试的一些代码更新答案。

首先在 服务器端:

   io.sockets.on('connection', function (sock) {    console.log('Connected client');    sock.emit('connected', {        connected: 'Yay!'    });    // the client passes 'callback' as a function. When we invoke the callback on the server    // the pre on the client side will run    sock.on('testmessage', function (data, callback) {        console.log('Socket (server-side): received message:', data);        var responseData = { string1: 'I like ', string2: 'bananas ', string3: ' dude!'        };        //console.log('connection data:', evData);        callback(responseData);    });});

客户端:

console.log('starting connection...');var socket = io.connect('http://localhost:3000');socket.on('error', function (evData) {    console.error('Connection Error:', evData);});// 'connected' is our custom message that let's us know the user is connectedsocket.on('connected', function (data) {    console.log('Socket connected (client side):', data);    // Now that we are connected let's send our test call with callback    socket.emit('testmessage', {        payload: 'let us see if this worketh'    }, function (responseData) {        console.log('Callback called with data:', responseData);    });});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417954.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号