-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 70a1bba
Showing
36 changed files
with
10,627 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" path="src/main/resources/templates"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>bootdemo</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
eclipse.preferences.version=1 | ||
encoding//src/main/java=UTF-8 | ||
encoding//src/main/resources=UTF-8 | ||
encoding//src/test/java=UTF-8 | ||
encoding//src/test/resources=UTF-8 | ||
encoding/<project>=UTF-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | ||
org.eclipse.jdt.core.compiler.source=1.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
activeProfiles= | ||
eclipse.preferences.version=1 | ||
resolveWorkspaceProjects=true | ||
version=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.muyi</groupId> | ||
<artifactId>bootdemo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.3.0.RELEASE</version> | ||
</parent> | ||
|
||
<name>bootdemo</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>springloaded</artifactId> | ||
<version>1.2.5.RELEASE</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-thymeleaf</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-websocket</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.muyi.bootdemo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import com.muyi.bootdemo.bean.Person; | ||
|
||
@Controller | ||
@SpringBootApplication | ||
@EnableAutoConfiguration | ||
public class Application { | ||
|
||
@RequestMapping("/Hello_World") | ||
String home(){ | ||
return "Hello World"; | ||
} | ||
|
||
@RequestMapping("/now") | ||
String hehe() { | ||
return "现在时间:" + (new Date()).toLocaleString(); | ||
} | ||
|
||
@RequestMapping("/") | ||
public String index(Model model) { | ||
Person single = new Person("aa", 11); | ||
List<Person> people = new ArrayList<>(); | ||
Person p1 = new Person("zhangsan", 11); | ||
Person p2 = new Person("lisi", 22); | ||
Person p3 = new Person("wangwu", 33); | ||
people.add(p1); | ||
people.add(p2); | ||
people.add(p3); | ||
model.addAttribute("singlePerson", single); | ||
model.addAttribute("people", people); | ||
return "index"; | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.muyi.bootdemo.bean; | ||
|
||
public class Person { | ||
|
||
private String name; | ||
private Integer age; | ||
|
||
public Person() { | ||
super(); | ||
} | ||
|
||
public Person(String name, Integer age) { | ||
super(); | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Integer getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(Integer age) { | ||
this.age = age; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
bootdemo/src/main/java/com/muyi/bootdemo/comment/MyWebSocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.muyi.bootdemo.comment; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.CopyOnWriteArraySet; | ||
|
||
import javax.websocket.OnClose; | ||
import javax.websocket.OnError; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.OnOpen; | ||
import javax.websocket.Session; | ||
import javax.websocket.server.ServerEndpoint; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@ServerEndpoint(value = "/websocket") | ||
@Component | ||
public class MyWebSocket { | ||
|
||
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 | ||
private static int onlineCount = 0; | ||
|
||
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。 | ||
private static CopyOnWriteArraySet<MyWebSocket> webSocketSet = new CopyOnWriteArraySet<MyWebSocket>(); | ||
|
||
//与某个客户端的连接会话,需要通过它来给客户端发送数据 | ||
private Session session; | ||
|
||
/** | ||
* 连接建立成功调用的方法*/ | ||
@OnOpen | ||
public void onOpen(Session session) { | ||
this.session = session; | ||
String userName = session.getQueryString(); | ||
System.out.println(userName); | ||
webSocketSet.add(this); //加入set中 | ||
addOnlineCount(); //在线数加1 | ||
System.out.println("有新连接加入!当前在线人数为" + getOnlineCount()); | ||
try { | ||
//sendMessage(CommonConstant.CURRENT_WANGING_NUMBER.toString()); | ||
sendMessage("有新连接加入!当前在线人数为:"+getOnlineCount()); | ||
} catch (IOException e) { | ||
System.out.println("IO异常"); | ||
} | ||
} | ||
|
||
/** | ||
* 连接关闭调用的方法 | ||
*/ | ||
@OnClose | ||
public void onClose() { | ||
webSocketSet.remove(this); //从set中删除 | ||
subOnlineCount(); //在线数减1 | ||
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount()); | ||
} | ||
|
||
/** | ||
* 收到客户端消息后调用的方法 | ||
* | ||
* @param message 客户端发送过来的消息*/ | ||
@OnMessage | ||
public void onMessage(String message, Session session) { | ||
System.out.println("来自客户端的消息:" + message); | ||
|
||
//群发消息 | ||
for (MyWebSocket item : webSocketSet) { | ||
try { | ||
item.sendMessage(message); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 发生错误时调用 | ||
*/ | ||
@OnError | ||
public void onError(Session session, Throwable error) { | ||
System.out.println("发生错误"); | ||
error.printStackTrace(); | ||
} | ||
|
||
|
||
public void sendMessage(String message) throws IOException { | ||
this.session.getBasicRemote().sendText(message); | ||
//this.session.getAsyncRemote().sendText(message); | ||
} | ||
|
||
|
||
/** | ||
* 群发自定义消息 | ||
* */ | ||
public static void sendInfo(String message) throws IOException { | ||
for (MyWebSocket item : webSocketSet) { | ||
try { | ||
item.sendMessage(message); | ||
} catch (IOException e) { | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
public static synchronized int getOnlineCount() { | ||
return onlineCount; | ||
} | ||
|
||
public static synchronized void addOnlineCount() { | ||
MyWebSocket.onlineCount++; | ||
} | ||
|
||
public static synchronized void subOnlineCount() { | ||
MyWebSocket.onlineCount--; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
bootdemo/src/main/java/com/muyi/bootdemo/config/WebMvcConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.muyi.bootdemo.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||
|
||
@Configuration | ||
public class WebMvcConfig extends WebMvcConfigurerAdapter{ | ||
|
||
@Override | ||
public void addViewControllers(ViewControllerRegistry registry) { | ||
// TODO Auto-generated method stub | ||
//super.addViewControllers(registry); | ||
registry.addViewController("/ws").setViewName("/ws"); | ||
registry.addViewController("/login.jsp").setViewName("/login"); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
bootdemo/src/main/java/com/muyi/bootdemo/config/WebSocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.muyi.bootdemo.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.socket.server.standard.ServerEndpointExporter; | ||
|
||
@Configuration | ||
public class WebSocketConfig { | ||
|
||
@Bean | ||
public ServerEndpointExporter serverEndpointExporter() { | ||
return new ServerEndpointExporter(); | ||
} | ||
} |
Oops, something went wrong.