最简单的Socket.io示例的示例是什么?

编程入门 行业动态 更新时间:2024-10-07 18:26:21
本文介绍了最简单的Socket.io示例的示例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,我最近一直在尝试了解Socket.io,但是我不是一个非常出色的程序员,几乎我可以在网上找到的每个示例(相信我已经花了数小时的时间)都包含一些额外的内容,使事情复杂化.许多示例都会使我感到困惑,或者连接到一些奇怪的数据库,或者使用coffeescript或大量的JS库将事情弄乱.

So, I have been trying to understand Socket.io lately, but I am not a supergreat programmer, and almost every example I can find on the web (believe me I have looked for hours and hours), has extra stuff that complicates things. A lot of the examples do a bunch of things that confuse me, or connect to some weird database, or use coffeescript or tons of JS libraries that clutter things up.

我很乐意看到一个基本的,可以正常运行的示例,其中服务器仅每10秒向客户端发送一条消息,说明现在几点,然后客户端将该数据写入页面或引发警报,很简单.然后,我可以从那里弄清楚事情,添加数据库连接之类的我需要的东西.是的,我已经检查了socket.io网站上的示例,但它们对我不起作用,我也不知道它们在做什么

I'd love to see a basic, functioning example where the server just sends a message to the client every 10 seconds, saying what time it is, and the client writes that data to the page or throws up an alert, something very simple. Then I can figure things out from there, add stuff I need like db connections, etc. And yes I have checked the examples on the socket.io site and they don't work for me, and I don't understand what they do.

推荐答案

编辑:我认为最好向任何人咨询出色的聊天示例在Socket.IO入门页面上.自从我提供了此答案以来,该API已得到了相当简化.话虽如此,这是针对较新的API的原始答案已更新的大小.

I feel it's better for anyone to consult the excellent chat example on the Socket.IO getting started page. The API has been quite simplified since I provided this answer. That being said, here is the original answer updated small-small for the newer API.

只是因为我今天感觉很好:

Just because I feel nice today:

<!doctype html> <html> <head> <script src='/socket.io/socket.io.js'></script> <script> var socket = io(); socket.on('welcome', function(data) { addMessage(data.message); // Respond with a message including this clients' id sent from the server socket.emit('i am client', {data: 'foo!', id: data.id}); }); socket.on('time', function(data) { addMessage(data.time); }); socket.on('error', console.error.bind(console)); socket.on('message', console.log.bind(console)); function addMessage(message) { var text = document.createTextNode(message), el = document.createElement('li'), messages = document.getElementById('messages'); el.appendChild(text); messages.appendChild(el); } </script> </head> <body> <ul id='messages'></ul> </body> </html>

app.js

var http = require('http'), fs = require('fs'), // NEVER use a Sync function except at start-up! index = fs.readFileSync(__dirname + '/index.html'); // Send index.html to all requests var app = http.createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(index); }); // Socket.io server listens to our app var io = require('socket.io').listen(app); // Send current time to all connected clients function sendTime() { io.emit('time', { time: new Date().toJSON() }); } // Send current time every 10 secs setInterval(sendTime, 10000); // Emit welcome message on connection io.on('connection', function(socket) { // Use socket to communicate with this particular client only, sending it it's own id socket.emit('welcome', { message: 'Welcome!', id: socket.id }); socket.on('i am client', console.log); }); app.listen(3000);

更多推荐

最简单的Socket.io示例的示例是什么?

本文发布于:2023-11-28 08:08:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   最简单   Socket   io

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!