Welcome to this Hackaton project I created for user hrushi on phpclasses.org. The idea was to create a web socket chat applicationthat could be logging to a database. So here is what you need to get this up and running. Please note the minimum required PHP version is 7.0- this is not because it wanted this but it is because of the dependencies this project has.
First thing you is installing composer on to your system. You can get composer here. Don't worry it might seem intimidating but it is not.
Installing the project using composer is hands down the easiest way to get started. This method will download the project from GitHuband automatically install its dependencies for you. Presuming you installed composer (step 1) execute the following commands on the command-line.
$ composer create-project johnnymast/mysql_websocket_chat chat
$ cd chat
In the above example I am using a mac so my prompt will display different then you if you are on windows.
If you download this package in a zip file from phpclasses.org you will have to extract the zip package to a location of your liking. Thenchange directory into that directory and execute the following command on your prompt.
$ composer install
In the above example I am using a mac so my prompt will display different then you if you are on windows.
This project can be split into two different components. The WebSocket server is the server.php
in the root directory. The second partis the frontend part located in public/index.php
. For the WebSocket server, there are two configuration options that you can configure in includes/config.php
.
This flag allows you to configure the WebSocket server's IP-address. By default the value 127.0.0.1
has been set.
This will configure what port the WebSocket server will listen on. The default value has been set to 8080
. You can change thisvalue if it clashes with other services running on your machine.
This server can run either with or without a database. By default i have disabled the use of a database server (ENABLE_DATABASE
) but you can enable it by switching the ENABLE_DATABASE
to true
in the includes/config.php file.
Flag | Description |
---|---|
DATABASE_HOST | The database username goes in here. By default this has been set to root . |
DATABASE_PORT | The database port goes in here. By default this has been set to 3306 . |
DATABASE_USERNAME | The database username goes in here. By default this has been set to root . |
DATABASE_PASSWORD | Enter the password to access the database there. By default this has been set to root . |
DATABASE_DB | Enter the name of the database here. By default this has been set to socket_chat . |
ENABLE_DATABASE | This flag will turn using the database on or off by setting its value to true or false . |
Please note if you enable the database make sure you update the credentials as well (see table above). Also, if you enable the database make sure you have imported database.sql into your database.
Change direction into the chat directory and fire up the server.
$ cd chat
$ php ./server.php
When you see no output and the command seems to hang that's when you know its running.
In the chat directory, you will find index.php. This file will be the client for your chat application. Make sure you set any web service its document root to the public/
folder. Alternatively, if you don't have access to a webserver you can also try using PHP'sbuild-in webserver.
$ cd public
$ php -S 127.0.0.1:8000
This will start an webserver on port 8000
Now open up 2 chat tabs and point them to localhost (or maybe a virtual host you configured) and chat away with your self.
If you want to test private chats you can single click any user in the user list on the right of the screen. Then type your messagein the message bar, this will send a private message only to that user.
If you wish to know what has changed in this version of Mysql WebSocket Chat you can always checkout the changelog here.
This package is created and maintained by Johnny Mast. For feature requests or suggestions you could consider sending me an e-mail.
Oh and if you've come down this far, you might as well follow me on twitter.
MIT License
Copyright (c) 2021 Johnny Mast
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
基于socket.io快速实现一个实时通讯应用 看起来是一篇超牛的文章。 Socket.IO Chat A simple chat demo for Socket.IO How to use $ npm i $ npm start And point your browser to http://localhost:3000. Optionally, specify a port by supp
Mysql的socket登录 通常情况下我们都会使用账号、密码登录,也就是(tcp/ip)的方式登录。 Mysql还存在另外一种登录方式:socket方式登录 二者区别:常规的账号密码是基于tcp、ip方式的登录,而socket就是利用mysql.sock作为主机和客户机在同一台机器上使用unix domain socket作为通信载体进行通信的。 socket使用场景:多实例部署的时候,
问题 zabbix启动web界面提示: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 解决 这是由于修改mysql服务的了socket文件mysql.sock位置,而导致无法通过mysql socket文件连接到mysql服务引起的,具体解决办法如下: # vim /etc/m
import com.fasterxml.jackson.databind.ObjectMapper; import entity.Result; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHa
在/usr/my.cnf配置socket路径, vi /usr/my.cnf 添加 socket = /var/lib/mysql/mysql.sock 用下面的命令登陆mysql,不需要用户名密码 mysql -S /var/run/mysqld/mysqld.sock
连接服务器IP:192.168.20.55及其端口号3002, 并接收数据 function handleWSReceive() { this.ws = new WebSocket("ws://192.168.20.55:3002"); // websocket接收IP地址端口 this.ws.onopen = function () { console.log('
package com.example.ewdatasystem_android.websocket; import com.example.util.LogUtil; import org.java_websocket.WebSocket; import org.java_websocket.client.WebSocketClient; import org.java_websocket.f
mysql create table user ( id int auto_increament primary key, account varchar(20) not null unique, passwd varchar(20) not null, phone varchar(20) not null unique ) 服务端程序 #include<stdio.h> #include<std
最近有个项目需要实时,首先想到的就是WebSocket,现在又掉进了一坑,就是数据Mysql数据库里的数据是第三方软件每一秒更新插入的,除了轮训定时器外,也想到触发器来通知数据变化发送到Web页面,但是这样就没必要用WebSocket了,但是Mysql触发器又调不到Java代码,请问各位大神,有好的方法么????
websocket如何实现聊天,心跳,通知 链接 https://github.com/qianbokeji/tcpServer 实现聊天,通知,心跳机制,无缝衔接到任何分布式系统