socket.io:发射按钮's属性值
socket.io: Emit button's attribute value on click?
我有多个具有groupName
属性的按钮。它们看起来像这样:
<a href="#" class="fireGroup btn btn-info btn-lg" groupName="SCENE_I">SCENE I</a>
<a href="#" class="fireGroup btn btn-info btn-lg" groupName="SCENE_II">SCENE II</a>
<a href="#" class="fireGroup btn btn-info btn-lg" groupName="SCENE_III">SCENE III</a>
我正试图弄清楚如何让socket.io在单击时发出链接的groupName值。因此,当点击第一个链接时,socket.io会发出"groupName-SCENE_I"
如何才能做到这一点?
你似乎想要类似于聊天的东西——点击链接就像用户向服务器发送消息,服务器会将消息发送到房间(我想是向其他用户?)
如果是这样的话,您应该看看这个例子:http://socket.io/get-started/chat/
你可以在客户端做这样的事情:
<html>
<head>
</head>
<body>
<a href="#" class="fireGroup btn btn-info btn-lg" groupName="SCENE_I">SCENE I</a>
<a href="#" class="fireGroup btn btn-info btn-lg" groupName="SCENE_II">SCENE II</a>
<a href="#" class="fireGroup btn btn-info btn-lg" groupName="SCENE_III">SCENE III</a>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
$(document).ready(function(){
var socket = io();
// listen to server events related to messages coming from other users. Call this event "newClick"
socket.on('newClick', function(msg){
console.log("got new click: " + msg);
});
// when clicked, do some action
$('.fireGroup').on('click', function(){
var linkClicked = 'groupName - ' + $(this).attr('groupName');
console.log(linkClicked);
// emit from client to server
socket.emit('linkClicked', linkClicked);
return false;
});
});
</script>
</body>
</html>
在服务器端,仍在考虑聊天的想法:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('./index.html');
});
io.on('connection', function(socket){
// when linkClicked received from client...
socket.on('linkClicked', function(msg){
console.log("msg: " + msg);
// broadcast to all other users -- originating client does not receive this message.
// to see it, open another browser window
socket.broadcast.emit('newClick', 'Someone clicked ' + msg) // attention: this is a general broadcas -- check how to emit to a room
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 全局变量和全局对象的属性之间有什么区别吗
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 序列化数据属性中对象的最可靠方法
- 分析高度属性时出现意外值{{specs.height}}.index.html
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- YUI3 IO实用程序是否可以根据给定的内容类型标头值自动序列化数据
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- AngularJS-使用'true'属性
- WebRTC,socket.io,node.js:无法读取属性'发射'的未定义
- socket.io:发射按钮's属性值
- 未捕获的类型错误: 无法读取未定义 Phaser.io 的属性“补间”
- Socket.io,Socket.set()和Socket属性之间的差异
- Trigger.io:调用location.reload()我们可以'请勿访问任何锻造属性
- 如何在Socket中订阅id以外的属性为真的实例室.io / SailsJS
- 未捕获的类型错误:无法读取未定义的 sails.io.js 的属性“原型”
- 在Socket.io中为XMLHttpRequest设置withCredentials属性