View Socket View

19 views Asked by At

I am trying to run a chat application with socket. I can't view this application at http://185.86.166.82:1337/client.php or http://localhost:1337 and I can't view the view and chat. http://185.86.166.82:1337/client.phpadresinde just says 'Welcome to socket.io.'.

server.js

var server     = require('http').createServer(),
    io         = require('socket.io')(server),
    logger     = require('winston'),
    port       = 1337;

// Logger config
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, { colorize: true, timestamp: true });
logger.info('SocketIO > listening on port ' + port);

io.on('connection', function (socket){
    logger.info('SocketIO > Connected socket ' + socket.id);

    socket.on('disconnect', function () {
        logger.info('SocketIO > Disconnected socket ' + socket.id);
    });

    socket.on('broadcast', function (message) {
        logger.info('ElephantIO broadcast > ' + JSON.stringify(message));
    });
});

server.listen(port);

client.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Socket.IO Example</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.5/socket.io.js">.                    </script>
</head>
<body>
    <h1>Socket.IO Example</h1>
    <div id="messages"></div>

    <script>
        const socket = io('http://185.86.166.82:1337');

        socket.on('message', (data) => {
            const messagesDiv = document.getElementById('messages');
            const messageParagraph = document.createElement('p');
            messageParagraph.textContent = data;
            messagesDiv.appendChild(messageParagraph);
        });
    </script>
</body>
</html>

chanels.php

use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

client.php

    use ElephantIO\\Client;
    use ElephantIO\\Engine\\SocketIO\\Version2X;

    require __DIR__ . '/../../../../vendor/autoload.php';

    $client = new Client(new Version2X('http://localhost:1337', \[
    'headers' =\> \[
    'X-My-Header: websocket rocks',
    'Authorization: Bearer 12b3c4d5e6f7g8h9i'
    \]
    \]));

    $client-\>initialize();
    $client-\>emit('broadcast', \['foo' =\> 'bar'\]);
    $client-\>close();

I perform the operations in the videos I watched, but I couldn't do it. This project was run before and the operation I wanted with these codes was performed. However, I couldn't get it to work. I also tried adding Cors, but it didn't work. I would be very happy if you can help me find out what I am missing by helping and directing me.

0

There are 0 answers