باید از سمت سرور اونو مشخص کنید. مثلا در سورس زیر دقت کنید.
// Setup basic express server
var express = require('express');
var app = express();
var path = require('path');
var server = require('http').createServer(app);
var io = require('../..')(server);
var port = process.env.PORT || 3000;
server.listen(port, () => {
console.log('Server listening at port %d', port);
});
// Routing
app.use(express.static(path.join(__dirname, 'public')));
// Chatroom
var numUsers = 0;
io.on('connection', (socket) => {
var addedUser = false;
// when the client emits 'new message', this listens and executes
socket.on('new_message', (data) => {
// we tell the client to execute 'new message'
socket.broadcast.emit('new_message', {
username: socket.username,
message: data
});
});
// when the client emits 'add user', this listens and executes
socket.on('add_user', (username) => {
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = username;
++numUsers;
addedUser = true;
socket.emit('login', {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user_joined', {
username: socket.username,
numUsers: numUsers
});
});
// when the client emits 'typing', we broadcast it to others
socket.on('typing', () => {
socket.broadcast.emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
socket.on('stop_typing', () => {
socket.broadcast.emit('stop_typing', {
username: socket.username
});
});
// when the user disconnects.. perform this
socket.on('disconnect', () => {
if (addedUser) {
--numUsers;
// echo globally that this client has left
socket.broadcast.emit('user_left', {
username: socket.username,
numUsers: numUsers
});
}
});
});
این کد Node.js مناسب و هماهنگ برای سرویس موجود در بیسیک می باشد.
همانطور که میبینید رویداد ها رو همانند نسخه بیسیک با On معرفی کرده اند
پورت رو هم 3000 قرار داده شده است.
منبع
«موفق باشید.»