javascript - Include socket.io in the script tag using html - Stack Overflow

admin2025-04-18  0

While using html for long time, we used to include the script tags calling for some file within the same folder of the html page, or any other folder but we have to include the exact source of it, something like that:

<script src = "the source of the file and its name"></script>

Using socket.io website, there is index.html file, and the script tag is like that:

<script src="/socket.io/socket.io.js"></script>

but in fact the real source of socket.io.js file is in the node modules, even-though the website is working well, if I include the real source of this file, the website will crash.

I am really curious about the reason of this strange situation, someone explain it to me, please!

While using html for long time, we used to include the script tags calling for some file within the same folder of the html page, or any other folder but we have to include the exact source of it, something like that:

<script src = "the source of the file and its name"></script>

Using socket.io website, there is index.html file, and the script tag is like that:

<script src="/socket.io/socket.io.js"></script>

but in fact the real source of socket.io.js file is in the node modules, even-though the website is working well, if I include the real source of this file, the website will crash.

I am really curious about the reason of this strange situation, someone explain it to me, please!

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 13, 2017 at 14:13 Zainab HammamiZainab Hammami 4312 gold badges6 silver badges15 bronze badges 13
  • Did you try the CDN? https://cdnjs./libraries/socket.io – Daniel Krom Commented Jun 13, 2017 at 14:16
  • I already have the library installed in the node modules, but I will try the link @DanielKrom – Zainab Hammami Commented Jun 13, 2017 at 14:17
  • you wrap your HTTP server in Socket.IO and it intercepts requests for "/socket.io/socket.io.js" and sends the appropriate response automatically. That is why <script src="/socket.io/socket.io.js"></script> works – Nikhil Nanjappa Commented Jun 13, 2017 at 14:18
  • I got this error: Refused to execute script from 'https://cdnjs./libraries/socket.io' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. @DanielKrom – Zainab Hammami Commented Jun 13, 2017 at 14:19
  • @ZainabHammami - Your Socket.IO server will handle serving the correct version of the Socket.IO client library; you should not be using one from elsewhere on the Internet. – Nikhil Nanjappa Commented Jun 13, 2017 at 14:21
 |  Show 8 more ments

1 Answer 1

Reset to default 5

The website crashes rightly because, your Socket.IO server will handle serving the correct version of the Socket.IO client library; you should not be using one from anywhere else.

How it works?

you wrap your HTTP server in Socket.IO like this:

var io = require('socket.io')(http);

and it intercepts requests for "/socket.io/socket.io.js" and sends the appropriate response automatically. That is why <script src="/socket.io/socket.io.js"></script> works and the others don't.

Meaning if the server is running, socket.io.js should be readily available.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744970226a277443.html

最新回复(0)