node SOCKET.iO的使用方法

福尔摩斯对我笑 2019-01-18 66661 次 10673条

服务器发送,客户端监听

  • 服务端安装 socket.io
 cnpm install socket.io --save
  • 客户端安装scoket.io.js文件,去socket.io官网下载js文件
<script src="/static/js/socket.io.js"></script>
  • 书写服务端代码
//引入io并监听2000端口
const io = require('socket.io')(2000);

//向客户端发送数据
setInterval(function(){
socket.emit(`del_files`, 'xxxxxxxx');
console.log(1)
},1000)

  • 书写客户端代码
//同样监听2000端口
var sockets = io.connect('http://localhost:2000');

sockets.on('del_files', function (data) {
alert(data);
})

好了,就是这么简单

客户端发送,服务器端监听

  • 引入方式同上面一样,这里就不写了
  • 客户端发送数据
setInterval(function(){
    socket.emit('req_server_cn', '1' );
},10000)
  • 服务器端监听
  io.sockets.on('connection',  function (socket) 
        {
        
            socket.on(`req_server_cn`, (data)=>{
              console.log(data)
            });
        });
  ``

好了,也是这么简单

标签: 无

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 10673 条评论 1


  1. balenciaga sneakers

    I want to point out my gratitude for your kindness supporting men and women who must have guidance on this important field. Your real dedication to passing the solution all around became incredibly productive and have in every case enabled ladies much like me to achieve their pursuits. Your interesting recommendations means a lot a person like me and a whole lot more to my mates. Regards; from everyone of us.

    回复 2019年10月24日
  2. air jordan

    Needed to create you the tiny remark to finally thank you as before on your magnificent basics you’ve contributed at this time. It has been certainly incredibly open-handed of people like you to deliver unreservedly what exactly many of us would’ve offered for an ebook to end up making some cash for themselves, certainly considering the fact that you might have done it in case you desired. These secrets also acted to become a easy way to recognize that other people online have the identical keenness really like my personal own to figure out a good deal more related to this issue. I am certain there are lots of more fun instances up front for people who browse through your website.

    回复 2019年10月23日
  3. nike air max95

    I truly wanted to type a brief comment in order to say thanks to you for all of the pleasant tips and hints you are posting on this site. My time consuming internet investigation has finally been compensated with high-quality strategies to talk about with my co-workers. I would assert that most of us site visitors are really fortunate to dwell in a magnificent community with many marvellous professionals with very helpful secrets. I feel extremely lucky to have come across your weblog and look forward to really more exciting minutes reading here. Thanks a lot once again for all the details.

    回复 2019年10月23日
  4. vans outlet

    Thank you so much for providing individuals with an extraordinarily superb possiblity to read critical reviews from this site. It really is very sweet plus stuffed with a great time for me personally and my office acquaintances to search your website not less than thrice in a week to study the fresh issues you will have. And lastly, I am at all times amazed with all the exceptional secrets served by you. Certain 2 areas in this article are undeniably the most beneficial I have ever had.

    回复 2019年10月23日
  5. adidas tubular

    My spouse and i have been now fulfilled Louis managed to carry out his studies by way of the precious recommendations he made from your very own web pages. It is now and again perplexing to simply possibly be giving away tips which many others could have been trying to sell. And we all discover we now have the blog owner to thank for this. Most of the explanations you’ve made, the easy website navigation, the friendships you can help to promote – it’s all excellent, and it is making our son and us understand this issue is cool, which is certainly quite important. Thanks for everything!

    回复 2019年10月22日
  6. nike hyperdunk

    I wanted to write you a little remark to give thanks the moment again with the marvelous tricks you’ve featured here. It’s so unbelievably open-handed with people like you to present unhampered precisely what many individuals could possibly have distributed for an e-book to get some money for themselves, most notably seeing that you could have tried it if you wanted. These inspiring ideas also served to provide a easy way to be sure that someone else have the identical fervor really like my own to realize significantly more regarding this condition. I know there are numerous more enjoyable times in the future for many who start reading your blog post.

    回复 2019年10月21日
  7. yeezy boost 350

    I precisely wanted to appreciate you all over again. I do not know the things that I would’ve used without these pointers discussed by you over such a topic. It was actually a very horrifying case in my view, but taking a look at this specialised form you treated that took me to leap over delight. I will be thankful for your help and even believe you know what a great job you were putting in educating people today using a blog. I know that you haven’t met all of us.

    回复 2019年10月20日
  8. nba jerseys

    I definitely wanted to write a brief comment to thank you for those pleasant facts you are writing at this website. My time consuming internet lookup has at the end been compensated with excellent knowledge to exchange with my visitors. I ‘d declare that we visitors are rather lucky to exist in a useful website with so many outstanding individuals with good secrets. I feel truly happy to have encountered the website page and look forward to some more thrilling times reading here. Thank you once again for a lot of things.

    回复 2019年10月19日
  9. fila shoes

    I simply had to thank you so much once again. I am not sure the things that I would have implemented in the absence of the type of basics documented by you about such a problem. It truly was a very terrifying issue in my position, however , coming across your professional way you handled that made me to cry for gladness. Now i’m thankful for this service and in addition have high hopes you recognize what a powerful job your are accomplishing educating many others thru your websites. Probably you haven’t got to know any of us.

    回复 2019年10月18日
  10. jordan shoes

    I definitely wanted to write a small word to express gratitude to you for the precious tips and hints you are writing on this site. My extended internet search has at the end been honored with awesome facts and techniques to share with my classmates and friends. I ‘d declare that most of us visitors actually are quite blessed to exist in a fantastic network with many lovely professionals with good solutions. I feel truly blessed to have seen your web pages and look forward to so many more awesome moments reading here. Thanks once again for everything.

    回复 2019年10月17日
  11. nike air max 90

    Thanks a lot for providing individuals with remarkably breathtaking opportunity to read critical reviews from here. It really is very pleasing and packed with a great time for me personally and my office mates to search the blog no less than 3 times in one week to study the latest guidance you will have. And indeed, I’m just always happy with the spectacular methods served by you. Certain two points in this posting are completely the best we’ve had.

    回复 2019年10月16日
  12. louboutin shoes

    I wanted to write down a simple word so as to thank you for all of the great points you are giving at this website. My considerable internet lookup has finally been compensated with reliable knowledge to talk about with my colleagues. I would mention that most of us site visitors are rather lucky to exist in a remarkable site with many outstanding people with good plans. I feel truly blessed to have seen your entire website page and look forward to many more thrilling times reading here. Thank you again for a lot of things.

    回复 2019年10月15日
  13. air max 270

    I together with my guys were looking at the great advice found on your web blog while at once developed an awful feeling I never thanked you for those techniques. All of the people became as a consequence glad to learn all of them and now have surely been having fun with those things. Many thanks for being so considerate as well as for figuring out this kind of decent subject matter millions of individuals are really eager to discover. Our honest regret for not saying thanks to earlier.

    回复 2019年10月14日
  14. golden goose sneakers

    I not to mention my pals appeared to be analyzing the best thoughts located on your web page and so all of a sudden came up with a terrible suspicion I had not thanked the web blog owner for those tips. Most of the men were definitely totally very interested to read all of them and now have in fact been having fun with those things. Thanks for genuinely really helpful as well as for utilizing some helpful things most people are really desperate to know about. Our sincere regret for not expressing appreciation to you earlier.

    回复 2019年10月13日
  15. adidas stan smith women

    I enjoy you because of all of your hard work on this blog. Kim delights in making time for internet research and it’s obvious why. Many of us hear all concerning the compelling way you give reliable tips and hints through your blog and as well as welcome response from some others about this area of interest then our princess is truly studying a great deal. Take pleasure in the remaining portion of the new year. You’re conducting a glorious job.

    回复 2019年10月12日
  16. nike foamposite

    I just wanted to make a word in order to say thanks to you for all of the fantastic strategies you are sharing at this site. My extensive internet look up has at the end been paid with incredibly good knowledge to write about with my close friends. I ‘d state that that most of us site visitors are rather blessed to dwell in a wonderful website with very many outstanding individuals with very beneficial tactics. I feel somewhat lucky to have discovered the web site and look forward to tons of more excellent minutes reading here. Thanks a lot again for everything.

    回复 2019年10月11日
  17. off white clothing

    I wanted to create you that little bit of word to be able to say thank you over again regarding the awesome techniques you’ve featured on this page. It’s so pretty generous with people like you to give unreservedly what exactly a lot of people could have offered as an ebook to help make some cash for themselves, mostly now that you could have done it if you ever decided. The basics as well worked to provide a easy way to recognize that the rest have a similar passion just like my personal own to understand good deal more with respect to this issue. I know there are millions of more pleasurable situations in the future for individuals who look over your site.

    回复 2019年10月10日
  18. nike air max 2017

    I and also my guys have already been viewing the nice ideas found on your website while the sudden I had an awful feeling I never expressed respect to the blog owner for those tips. The men were definitely as a consequence passionate to study them and already have pretty much been using these things. Appreciation for really being so helpful and for utilizing such essential resources millions of individuals are really desperate to be aware of. Our own sincere regret for not expressing appreciation to you sooner.

    回复 2019年10月9日
  19. hermes

    I precisely desired to say thanks yet again. I’m not certain the things that I might have achieved in the absence of these ideas provided by you about such concern. Entirely was the horrifying concern in my view, however , noticing the skilled manner you processed the issue forced me to cry with delight. I’m thankful for the advice as well as trust you find out what a powerful job you are always putting in teaching others through your site. I am sure you’ve never come across all of us.

    回复 2019年10月8日
  20. jimmy choo shoes

    Thank you so much for providing individuals with such a marvellous opportunity to read articles and blog posts from here. It’s usually so brilliant and jam-packed with a great time for me and my office acquaintances to search your web site really 3 times in one week to find out the fresh guidance you have got. And indeed, I am always amazed with the unique knowledge you serve. Certain 3 tips on this page are in truth the most suitable we’ve ever had.

    回复 2019年10月7日
  21. 之间

    不用登录吗

    回复 2019年3月9日
  22. 赵先生

    谢谢您添加了我的友链 我已经添加您的网站

    回复 2019年3月5日