`require 'socket.io-client.js'` not working

前端 未结 3 870
生来不讨喜
生来不讨喜 2020-12-09 12:26

I was able to get the basic socket.io server application running on my own server, and request it directly through any web browser (I tried FF, chrome, and IE7 which all wor

3条回答
  •  悲哀的现实
    2020-12-09 13:05

    I use browserify to manage all the require() resources for the browser-side code.

    That said, I was able to require the socket.io-client on my browser-side code in the following way:

    var io = require('socket.io-client');
    var $ = require('jquery');
    
    var socket = io();
    $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
    });
    socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
    });
    

    As opposed to the following snippet using a traditional script tag format found on socket.io's github example: https://github.com/rauchg/chat-example/blob/master/index.html

    
    
    
    

    socket.io-client can be downloaded in your development environment as a node modulo by doing:

    npm install socket.io-client
    

提交回复
热议问题