Giter VIP home page Giter VIP logo

chat-example's Introduction

chat-example's People

Contributors

rauchg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chat-example's Issues

Local files instead of CDN link in chat "Getting Started" instructions

The getting started guide switches from locally hosted files, when initially adding scripts to the html, to using a CDN. Here's the problematic spot, which should include a link to the CDN rather than local files:

Now in index.html I add the following snippet before the :

<script src="/socket.io/socket.io.js"></script>
<script>
 var socket = io();
</script>

Uncaught TypeError: undefined is not a function

I get this error when changing a single line in your index.html

var socket = io();

to

var socket = new WebSocket('ws://192.168.1.174:10080/mcu');

I am not using Node.js, but my server does support websockets. Any idea you may have as to how I can fix this would be awesome.

screenshot from 2014-06-11 11 18 24

For those having trouble with logging messages to console.

It took me an embarrassingly long time to figure this out. When you include jQuery, change the script tag to a secured link. This fixed all my problems because jQuery wouldn't load in Chrome, making the jQuery we wrote before the body useless.

https://code.jquery.com/jquery-2.1.4.min.js

Edit: I'd like to add, I was following this tutorial on cloud9. Get requests are done with https, meaning I needed jQuery included using https. So this may not apply to those connecting via localhost:3000

Form submission not stopped (OSX + Chrome)

Hi,
I was programming the index.html page and I ran across an error where the return false in the $('form').submit(callback) did not actually stop the callback seen in this block:

$('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
});

For anyone else hitting this problem I remedied it by creating a helper method and attaching it to the button. Instead of using a form element I wrapped the "form" in a div element. the code below is what worked for me to complete the example:

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      div { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      div input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      div button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
    <script src="/socket.io/socket.io.js"></script>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script>
      var socket = io();

     // new code replacing the $('form').submit(...) bit
      function pushMessage () {
        var mess = $('#m').val();
        $('#m').val('');
        socket.emit('chat message', mess);
      }

      socket.on('chat message', function (msg) {
        $('#messages').append($('<li>').text(msg));
      });

    </script>
  </head>
  <body>
    <ul id="messages"></ul>
    <div>
      <input id="m" autocomplete="off" />
      <button onclick="pushMessage()">Send</button>
    </div>
  </body>
</html>

failed chat message event handling

In your example on http://socket.io/get-started/chat/, in index.html, the code for binding the event http://socket.io/get-started/chat/ etc is in the lines BEFORE the form is declared <form action=""> <input id="m" autocomplete="off" /><button>Send</button> </form> Which causes it to fail unless reordered! The <script> section for that code ought to go after the form has been created.

socket.io error on client side

I am getting this error while loading index.html. Need help.

The connection to ws://localhost:3000/socket.io/?EIO=3&transport=websocket&sid=zDCPxairx0Xm_Jk2AAAB was interrupted while the page was loading.

socket.on('chat message') does not get triggered

I am able to successfully create socket connection and print chat message on Node.js console. Which confirms that sending message via socket works. However I am not able to make the
socket.on('chat message' event work on HTML.

Note: There is no error shown in browser console

io.on not sending console.log

I have this in package

   {
      "name": "socket-chat-example",
      "version": "0.0.1",
      "description": "my first socket.io app",
      "dependencies": {
        "express": "^4.10.2",
        "socket.io": "^1.3.5"
     }
}

this in my html

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>

  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>
  <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(document).ready(function(){
  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));
  });
});
</script>
</html>

this is in index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/',function(req,res){
    res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
    socket.on('chat message', function(msg){
        io.emit('chat message: ', msg);
    });
});

http.listen(3000,function(){
    console.log('lisenting on *:3000');
});

For some reason the io.on in index.js wasn't writing to console.log in the middle of tut. and i assumed it was something wrong with windows since alot of console.log doesn't show in CMD.

Anyways does anyone have clue as to why this isn't working? has something depreciated in socket.io that doesn't allow this tutorial to work anymore or is it something specific to windows machines? Thanks for your response

Failed to load resource

Hello there I'm working with:

Ubutu 14.04
Express 4.10.2
Socket.io 1.3.7

Vagrant
Port fowarding :8001 to Box :80
Port fowarding :8002 to Box :3000

This is my App.js

var express = require('express');
var app = express();
var server = app.listen(3000);

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

io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});

and this is my index.html

<!doctype html>

<title>Socket.IO chat</title> <style> \* { margin: 0; padding: 0; box-sizing: border-box; } body { font: 13px Helvetica, Arial; } form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } #messages { list-style-type: none; margin: 0; padding: 0; } #messages li { padding: 5px 10px; } #messages li:nth-child(odd) { background: #eee; } </style>
    Send <script src="https://cdn.socket.io/socket.io-1.3.7.js"></script> <script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script> var socket = io.connect(); </script>

    then when I open in the browser the index page I get:

    Failed to load resource: the server responded with a status of 404 (Not Found)
    http://127.0.0.1:8001/socket.io/?EIO=3&transport=polling&t=1451934295598-10

    any idea how to solve it?

    jquery src link

    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    It should be served over https.

    EDIT: I see there is already a PR (#30) for this... Please merge it.

    Filepath error using sendFile instead of sendfile

    The tutorial is inconsistent about using res.sendfile or res.sendFile. If res.sendFile is used (since sendfile is deprecated), using

    res.sendFile('index.html');

    doesn't work, throwing an error about the path not being specified properly. Here's what I did to fix it:

    res.sendFile(__dirname + '/index.html');

    http.listen vs app.listen

    In the index.js the app object is being passed into http.Server(), which is later on called with http.listen(). Is there any difference or upside to this method over just using express' builtin app.listen();? Why and how does this work?

    How to include socket.io.js (client) file in html?

    Hello,

    I want to include socket.io.js (client) file into index.html without using CDN. I have put socket.io.js into project's root directory. But when I'm trying to open the chat on browser, I'm getting 404 for that file. Here is my cod to include that file:

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

    File have full permission.

    Thanks

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google โค๏ธ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.